Date and time function examples for ASP website development
Function syntax description example
Now Now() Gets the current date and time of the system Dim MyVar MyVar = Now
'' MyVar contains the current date and time.
Date Date() Gets the current date of the system Dim MyDate MyDate = Date
'' MyDate contains the current system date.
Time Time() gets the current system time Dim MyTime MyTime = Time
'' Returns the current system time.
Year Year(Date) Gets the year of the given date Dim MyDate, MyYear
MyDate = #October 19, 1962#
Function syntax description example
Now Now() Gets the current date and time of the system Dim MyVar MyVar = Now
'' MyVar contains the current date and time.
Date Date() Gets the current date of the system Dim MyDate MyDate = Date
'' MyDate contains the current system date.
Time Time() gets the current system time Dim MyTime MyTime = Time
'' Returns the current system time.
Year Year(Date) Gets the year of the given date Dim MyDate, MyYear
MyDate = #October 19, 1962#
''Assign a date.
MyYear = Year(MyDate)
'' MyYear contains 1962.
Month Month(Date) Gets the month of the given date Dim MyVar MyVar = Month(Now)
'' MyVar contains the number corresponding to the current month.
Day Day(Date) Get the date of the given date Dim MyDay MyDay = Day(October 19, 1962)
''MyDay contains 19.
Hour Hour(time) Gets the hour of the given time Dim MyTime, MyHour
MyTime = Now MyHour = Hour(MyTime)
'' MyHour contains a numerical value representing the current time.
Minute Minute(time) Gets the minute Dim MyVar of the given time
MyVar = Minute(Now)
Second Second(time) Get the number of seconds of the given time Dim MySec
MySec = Second(Now)
''MySec contains a number representing the current second.
WeekDay WeekDay(Date) Gets the integer of the day of the week for the given date, 1 means Sunday, 2 means Monday, and so on Dim MyDate, MyWeekDay
MyDate = #October 19, 1962#
'' Assignment date
MyWeekDay = Weekday(MyDate)
'' MyWeekDay contains 6, MyDate represents Friday
DateDiff DateDiff(Var,Var1,Var2)
Var: date or time interval factor, with the following parameters:
yyyy year m month d day ww week h hour s second Var1: the first date or time
Var2: The second date or time, later than Var1, calculates the interval between two dates or times DateDiff(d,Date(),#1/1/2005#)
''Return how many days are left until New Year's Day 2005
DateDiff(h,Date(),#1/1/2005#)
''Return how many hours are left until New Year's Day 2005
DateDiff(d,#1/1/2003#,#1/1/2005#)
''Returns the number of days between two dates
DateAdd DateDiff(Var,Var1,Var2)
Var: date or time interval factor:
Var1: date or time interval multiple
Var2: Base of date or time. Add two dates or times. The following example adds one month to January 31, 1995:
NewDate = DateAdd(m, 1, 31-Jan-95)
In this example, DateAdd returns February 28, 1995, not February 31, 1995. If the date is January 31, 1996, February 29, 1996 is returned because 1996 is a leap year.
If the calculated date is before 100 AD, an error will occur.
FormatDateTime FormatDateTime(Date,vbShortDate) Convert to short date format FromatDateTime(Date(),vbLongDate)
Display in long date format
FormatDateTime(Date,vbLongDate) Convert to long date format
FormatDateTime(Date,vbShortTime) Convert to short time format
FormatDateTime(Date,vbLongTime) Convert to long time format