Introduction to _asp of date time functions in ASP

Source: Internet
Author: User
Tags date1 numeric value
Basic Functions

Date () Get dates, format: 2004-2-28
Time () to get the date, format: 22:24:59
Now () Get date and time format: 2005-5-2 22:37:30

How to use:

D=date ()
Get Year: Years (d)
Get month: month (d)
Get Days: Day (d)
Get Day of the week: weekday (d)

T=time ()
Get hours: hour (t)
Get minutes: Minute (t)
Get seconds: second (t)

========================================

Date Add function: DateAdd (interval, number, date)
You can use this function to add or subtract a specified time interval from a date.

parameter Description:

Base interval for interval-operations
number-Plus how many, can be negative
date-Base Date

Here is an optional value for interval, note with quotes:

Set up Description
yyyy Years
Q Quarter
M Month
Y The number of days in a year
D Day
W Number of days in a week
Ww Week
H Hours
N Minutes
S Seconds

Example:
1. Calculate Tomorrow:
Tomorrow=dateadd ("D", 1,date ())
2, the day before December 1, 2012:
Preday=dateadd ("D",-1, "2012-12-1")

Please search for more detailed usage.

========================================
Date interval function-datediff (interval, date1, date2[, firstdayofweek][, FirstWeekOfYear])
Calculates the gap between two dates.
For example, you can use DateDiff to calculate the number of days between two dates, or to calculate how many weeks from today to the end of the year. If the date1 is later than Date2, a negative number is returned.

Parameter description:

interval-with DATEADD () function
Date1,date2-Two dates for operations

Optional Parameters:

firstdayofweek-specifies a constant for the first day of the one week. If not specified, Sunday is the first day.
This parameter affects the result when the two-date difference is calculated several weeks (the time interval symbol "W" or "ww"). Please search for specific.
firstweekofyear-specifies how the first week of the year is determined and, if not specified, the first week of the week containing January 1. Please search for specific.

========================================
Other functions:
DateSerial (year, month, day)-converts numeric values to date variables and returns the same value type as the date () function. For example, if day=35, the next month is automatically calculated. For example, DateSerial (1990-10, 8-2, 1-1) calculates the date before the 10 year, 2 month, and day of August 1, 1990, which is May 31, 1980.
DatePart (interval, date[,firstdayofweek[, firstweekofyear])-Returns the numeric value of the specified time section. For example, you can use DatePart to calculate the day of the week or the current clock. Parameters are the same as above.
DateValue (date)-converts various date format strings to date variables and returns the same value type as the date () function. For example, in addition to the 12/30/1991 and 12/30/91, DateValue also identified December 30, 1991 and Dec 30, 1991.
TimeValue-Converts various time format strings into time variables. Principle with DateValue.
TimeSerial (hour, minute, second)-Converts a numeric value to a time variable, and returns the same value type as the Times () function. Principle with dateserial.
Timer ()-The number of seconds from midnight to the current elapsed time.
The following are examples of usage:
Now () gets the current system date and time, which can be written by the ASP output: <%=now ()%>
Year (now ()) to get years, ASP output: <%=year (now ())%>
Month (now ()) Gets the current month, ASP output: <%=month (Today ())%>
Day (now ()) Gets the number of days, ASP output: <%=day (now ())%>
Minute (now ()) Gets the number of minutes, ASP output: <%=minute (now ())%>
Second (now ()) Gets the number of seconds, ASP output: <%=second (now ())%>
Date () Gets the current system day, in the format: 2004-2-28
Time () Gets the current system date in the format: 22:24:59
DateAdd function (Time backward)
DATEADD ("H", 13,now ()) where "H" stands for the hour, 13 delegates plus 13 hours, now () is the current date time, or "H" can be changed:
Set description
YYYY years
Q Quarter
M month
Y number of days in a year
D-Day
W Number of days in a week
WW Week
H hours
N Minutes
s seconds
Subtract a certain number of days by the time interval method
Copy Code code as follows:

Function Date1 ()
Dim NewTime as DateTime
NewTime = DateTime.Now.Subtract (New TimeSpan (7, 0, 0, 0))
NewTime = Newtime.format ("mm/dd/yyyy", DateTimeFormatInfo)
Response.Write (NewTime)
End Function

Subtract a certain number of days by using a method that adds a negative number of days
Copy Code code as follows:

Public Function Date2 ()
Dim NewTime as DateTime
NewTime = DateTime.Now.AddDays (-7)
Dim s As String = NewTime
return s
End Function

Return Date name string
Copy Code code as follows:

Function Date3 ()
Dim newtime as DateTime = Now.adddays (-7)
Response.Write (System.String.Format ("{0:mm/dd/yyyy}", NewTime))
End Function
Function Date4 ()
Dim NewTime as DateTime
NewTime = Now.adddays (-7)
Return newtime.tostring ()
End Function

Use long time mode
Copy Code code as follows:

Public Function Date5 ()
Dim NewTime as DateTime
NewTime = Now ()
Return newtime.tolongtimestring ()
End Function

Use short time mode
Copy Code code as follows:

Public Function Date6 ()
Dim NewTime as DateTime
NewTime = Now ()
Return newtime.toshorttimestring ()
End Function

Use long Date mode
Copy Code code as follows:

Public Function Date7 ()
Dim NewTime as DateTime
NewTime = Now ()
Return newtime.tolongdatestring ()
End Function

Use the short day mode
Copy Code code as follows:

Public Function Date8 ()
Dim NewTime as DateTime
NewTime = Now ()
Return newtime.toshortdatestring ()
End Function

Format Date function
Format to General format
Copy Code code as follows:

Function Date9 ()
Dim NewTime as DateTime
NewTime = DateTime.Now.Subtract (New TimeSpan (7, 0, 0, 0))
Return FormatDateTime (newtime, 0)
End Function

Format Growth Date format
Copy Code code as follows:

Function Date10 ()
Dim NewTime as DateTime
NewTime = DateTime.Now.Subtract (New TimeSpan (7, 0, 0, 0))
Return FormatDateTime (NewTime, 1)
End Function

format as Short date format
Copy Code code as follows:

Function Date11 ()
Dim NewTime as DateTime
NewTime = DateTime.Now.Subtract (New TimeSpan (7, 0, 0, 0))
Return FormatDateTime (NewTime, 2)
End Function

Format Growth Time format
Copy Code code as follows:

Function Date12 ()
Dim NewTime as DateTime
NewTime = DateTime.Now.Subtract (New TimeSpan (7, 0, 0, 0))
Return FormatDateTime (NewTime, 3)
End Function

Format into a short time format
Copy Code code as follows:

Function Date13 ()
Dim NewTime as DateTime
NewTime = DateTime.Now.Subtract (New TimeSpan (7, 0, 0, 0))
Return FormatDateTime (NewTime, 4)
End Function

Show the time of year, month, day, etc. separately
Show Date
Copy Code code as follows:

Function Date14 ()
Dim newtime as DateTime = Now.adddays (-7)
Dim s As String
s = (System.String.Format ("{0:dddd}", NewTime))
return s
End Function

Returns the day ordinal of the week of the date
Copy Code code as follows:

Function Date15 ()
Dim MyDate as DateTime
Dim Myweekday as Integer
MyDate = Now.adddays (-5)
Myweekday = Weekday (mydate)
Return Myweekday
End Function

Returns the month value
Copy Code code as follows:

Function Date16 ()
Dim MyDate as DateTime
Dim Mymonth as Integer
MyDate = Now.adddays (-5)
Mymonth = Month (mydate)
Return Mymonth
End Function

Returns a formatted string
Copy Code code as follows:

Function Date17 ()
Dim MyDate as String
MyDate = Format (now (), "yyyy")
Return mydate
End Function

DateDiff function
Returns a Variant (long) that represents the number of time intervals between two specified dates.
Grammar
DateDiff (interval, date1, date2[, firstdayofweek][, FirstWeekOfYear])
The following named arguments are in the DateDiff function syntax:
Partial description
Interval necessary. A string expression that represents the interval used to calculate the time difference between Date1 and Date2
Date1-date2 necessary; Variant (Date). The two dates to be used in the calculation.
FirstDayOfWeek Optional. Specifies the constant for the first day of the one week. If not specified, Sunday is the first day.
FirstWeekOfYear Optional. A constant that specifies the first week of the year. If not specified, the week that contains January 1 is the first week.

Set up
The interval parameter is set to the following values:
Set description
YYYY years
Q season
M month
Y number of days in a year
D-Day
W Number of days in a week
WW Week
H time
N Minutes
s seconds

The FirstDayOfWeek parameter is set to the following values:
Constant numerical description
Vbusesystem 0 uses the NLS API settings.
Vbsunday 1 week day (default)
Vbmonday 2 weeks A
Vbtuesday 3 week Two
Vbwednesday 4 week Three
Vbthursday 5 week Four
Vbfriday 6 Friday
Vbsaturday 7 Saturday
Constant numerical description
Vbusesystem 0 is set with the NLS API.
VbFirstJan1 1 starts with the week that contains January 1 (the default value).
vbFirstFourDays 2 begins the week of the first half of its week in the new year.
Vbfirstfullweek 3 begins with the first straddling week.
Description
The DateDiff function can be used to determine the number of time intervals specified between two dates. For example, you can use DateDiff to calculate the number of days between two dates, or to calculate how many weeks from today to the end of the year.
To calculate the number of days between Date1 and Date2, you can use the number of days of the year (Y) or day (d). When interval is a "day of the Week" (W), DateDiff returns the number of weeks for the two-day period. If Date1 is Monday, DateDiff calculates the number of Monday to Date2. This number contains date2 but does not contain date1. However, if interval is "Week" (WW), the DateDiff function returns the number of calendar weeks for the two-day period. Calculated from the number of Sunday between Date1 and Date2. If the date2 happens to be Sunday, the date2 will also be added to the DateDiff's count result, but it will not be counted whether date1 is Sunday or not.
If Date1 comes later than Date2, the return value of the DateDiff function is negative.
The FirstDayOfWeek parameter affects the results of using the time interval symbol "W" or "WW" calculations.
If Date1 or date2 is a date literal, the specified year becomes a fixed portion of that date. However, if date1 or date2 are enclosed in double quotes ("") and the year is slightly omitted, the current year is inserted into the code each time the expression date1 or date2 is evaluated. This allows you to write program code that applies to different years.
In calculating the year difference for January 1 of December 31 and the following year, DATEDIFF returns 1 to indicate a difference of one year, although in fact it is only a day difference.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.