SoFunction
Updated on 2025-04-08

Introduction to date and time functions in asp

Basic functions

date() gets the date, format: 2004-2-28
time() Get time, format: 22:24:59
now() Get date and time Format: 2005-5-2 22:37:30

How to use:

d=date()
Get Year: year(d)
Get month: month(d)
Get the day: day(d)
Get the weekday(d)

t=time()
Get the hour: hour(t)
Get minutes: minute(t)
Get seconds: second(t)

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

DateAdd(interval, number, date)
This function is used to add or subtract the specified time interval from the date.

Parameter description:

interval-The reference interval of the operation
number-plus how much can be negative
date-Basic date

Below are optional values ​​for interval, note that they are quoted:

set up describe
yyyy Year
q Quarterly
m moon
y Number of days in one year
d day
w Number of days of the week
ww week
h Hour
n minute
s Second

For example:
1. Calculate tomorrow:
tomorrow=DateAdd("d",1,date())
2. The day before December 1, 2012:
preday=DateAdd("d",-1,"2012-12-1")

For more detailed usage, please search.

========================================
Day interval function - DateDiff(interval, date1, date2[, firstdayofweek][, firstweekofyear]])
Calculate the interval between the two dates.
For example, you can use DateDiff to calculate how many days apart between two dates, or how many weeks there will be between today and the end of the year. If date1 is later than date2, a negative number is returned.

Parameter description:

interval-Same DateAdd() function
Date1,date2 - Two dates to perform the operation

Optional parameters:

firstdayofweek - Specifies the constant for the first day of a week. If not specified, Sunday is the first day.
This parameter affects the result when two dates differ by weeks (the interval symbol “w” or “ww”). Please search for details.
firstweekofyear - How to determine the first week of the year, if not specified, the week containing January 1st is the first week. Please search for details.

========================================
Other functions:
DateSerial(year, month, day) - converts the numeric value into a date variable, and returns the same value type as the date() function. For example, if day=35, it will automatically calculate until next month. For example, DateSerial(1990 - 10, 8 - 2, 1 - 1) calculates the date before ten years, two months and another day on August 1, 1990, that is, May 31, 1980.
DatePart(interval, date[, firstdayofweek[, firstweekofyear]]) - Returns the value of the specified time part. For example, you can use DatePart to calculate the day of the week or what time it is currently. The parameters are the same as above.
DateValue(date) - converts various date format strings into date variables, and returns the same value type as date() function. For example, in addition to 12/30/1991 and 12/30/91, DateValue also identifies December 30, 1991 and Dec 30, 1991.
TimeValue(time) - Converts various time format strings into time variables. The principle is the same as DateValue.
TimeSerial(hour, minute, second) - converts a numeric value into a time variable, the return value type is the same as the time() function. The principle is the same as DateSerial.
Timer() - The number of seconds elapsed from midnight to now.
Here are examples of use:
Now() gets the current system date and time. The asp output can be written like this: <%=now()%>
Year(now()) Get the year, ASP output: <%=Year(now())%>
Month(now()) Gets the current month, ASP output: <%=Month(now())%>
day(now()) Get the number of days, ASP output: <%=day(now())%>
Minute(now()) Get the minutes, ASP output: <%=Minute(now())%>
Second(now()) Get the number of seconds, ASP output: <%=Second(now())%>
date() gets the current system date, format: 2004-2-28
time() Gets the current system time, format: 22:24:59
DateAdd function (time backward)
DateAdd("h",13,Now()) where "h" represents hour, 13 represents 13 hours, now() is the current date and time, or "h" can be changed:
Setting Description
yyyy year
q Quarterly
m month
y Number of days in one year
d Day
w Number of days of a week
ww
h hours
n minutes
s seconds
Subtract a certain number of days by time interval
Copy the codeThe code is as follows:

Function Date1()
Dim NewTime as DateTime
NewTime = ( New TimeSpan(7, 0, 0, 0) )
NewTime = ("MM/dd/yyyy", DateTimeFormatInfo)
(NewTime)
End Function

Use the method of adding negative days to subtract a certain number of days
Copy the codeThe code is as follows:

Public Function Date2()
Dim NewTime as DateTime
NewTime = (-7)
Dim s as string = NewTime
return s
End Function

Returns the date name string
Copy the codeThe code is as follows:

Function Date3()
Dim NewTime as DateTime = (-7)
(("{0:MM/dd/yyyy}",NewTime))
End Function
Function Date4()
Dim NewTime as DateTime
NewTime = (-7)
return ()
End Function

Use long-term mode
Copy the codeThe code is as follows:

Public Function Date5()
Dim NewTime as DateTime
NewTime = Now()
return ()
End Function

Use short time mode
Copy the codeThe code is as follows:

Public Function Date6()
Dim NewTime as DateTime
NewTime = Now()
return ()
End Function

Use long date mode
Copy the codeThe code is as follows:

Public Function Date7()
Dim NewTime as DateTime
NewTime = Now()
return ()
End Function

Use short day mode
Copy the codeThe code is as follows:

Public Function Date8()
Dim NewTime as DateTime
NewTime = Now()
return ()
End Function

Format date function
Format into a general format
Copy the codeThe code is as follows:

Function Date9()
Dim NewTime as DateTime
NewTime = ( New TimeSpan(7, 0, 0, 0) )
return formatdatetime(NewTime, 0)
End Function

Format the growth date format
Copy the codeThe code is as follows:

Function Date10()
Dim NewTime as DateTime
NewTime = ( New TimeSpan(7, 0, 0, 0) )
return formatdatetime(NewTime, 1)
End Function

Format into short date format
Copy the codeThe code is as follows:

Function Date11()
Dim NewTime as DateTime
NewTime = ( New TimeSpan(7, 0, 0, 0) )
return formatdatetime(NewTime, 2)
End Function

Format long time format
Copy the codeThe code is as follows:

Function Date12()
Dim NewTime as DateTime
NewTime = ( New TimeSpan(7, 0, 0, 0) )
return formatdatetime(NewTime, 3)
End Function

Format into short-term format
Copy the codeThe code is as follows:

Function Date13()
Dim NewTime as DateTime
NewTime = ( New TimeSpan(7, 0, 0, 0) )
return formatdatetime(NewTime, 4)
End Function

Show the year, month, day and other parts separately
Show date
Copy the codeThe code is as follows:

Function Date14()
Dim NewTime as DateTime = (-7)
dim s as string
s = (("{0:dddd}",NewTime))
Return s
End Function

Return to the date of what day of the week
Copy the codeThe code is as follows:

Function Date15()
Dim MyDate as DateTime
Dim MyWeekDay as Integer
MyDate = (-5)
MyWeekDay = Weekday(MyDate)
return MyWeekDay
End Function

Return to month value
Copy the codeThe code is as follows:

Function Date16()
Dim MyDate as DateTime
Dim MyMonth as Integer
MyDate = (-5)
MyMonth = Month(MyDate)
return MyMonth
End Function

Returns a formatted string
Copy the codeThe code is as follows:

Function Date17()
Dim MyDate as String
MyDate = Format(Now(), "yyyy")
return MyDate
End Function

DateDiff function
Returns the value of Variant (Long) indicating the number of time intervals between two specified day periods.
grammar
DateDiff(interval, date1, date2[, firstdayofweek][, firstweekofyear]])
The following named parameters are included in the DateDiff function syntax:
Part Description
interval is necessary. String expression, representing the time 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 is optional. Specifies the constant for the first day of a week. If not specified, Sunday is the first day.
firstweekofyear optional. Specifies the constant for the first week of the year. If not specified, the week containing January 1 is the first week.

set up
The setting values ​​of the interval parameter are as follows:
Setting Description
yyyy year
Q Season
m month
y Number of days in one year
d Day
w Number of days of a week
ww
h
n minutes
s seconds

The set values ​​of the firstdayofweek parameter are as follows:
Constant Value Description
vbUseSystem 0 Use the NLS API settings.
vbSunday 1 Sunday (default)
vbMonday 2 Monday
vbTuesday 3 Tuesday
vbWednesday 4 Wednesday
vbThursday 5 Thursday
vbFriday 6 Friday
vbSaturday 7 Saturday
Constant Value Description
vbUseSystem 0 is set with the NLS API.
vbFirstJan1 1 starts from the week containing January 1 (default).
vbFirstFourDays 2 starts with the first of its most half week in the week of the new year.
vbFirstFullWeek 3 starts with the first week without inter-year.
illustrate
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 how many days apart between two dates, or how many weeks there will be between today and the end of the year.
To calculate the number of days where date1 and date2, you can use "Days of a Year" (y) or "Days" (d). When interval is "days of a week" (w), DateDiff returns the number of weeks during the two-day period. If date1 is Monday, DateDiff calculates the number of Mondays until date2. This number contains date2 but does not contain date1. However, if the interval is "week" (ww), the DateDiff function returns the number of "calendar weeks" during the two-day period. It is obtained by calculating the number of Sundays between date1 and date2. If date2 happens to be Sunday, date2 will also be added to the DateDiff count result; however, no matter whether date1 is Sunday or not, it will not be counted.
If date1 comes later than date2, the return value of the DateDiff function is negative.
The firstdayofweek parameter affects the results of the calculation using the time interval symbol "W" or "WW".
If date1 or date2 is a date text, the specified year becomes a fixed part of that date. However, if date1 or date2 is enclosed in double quotes (" ") and the year is skipped, the current year is inserted into the code each time the expression date1 or date2 is calculated. This allows you to write program code suitable for different years.
When calculating the year difference between December 31 and January 1 of the following year, DateDiff returns 1 to indicate a year difference, although it is actually only one day.