VBScript supports various date and time functions. In this section you will learn all supported date and time function.
Now Function
Now Function returns the current system date along with the time. The following example uses the Now function to show the message for the current date and time.
Dim currentDateTime currentDateTime = Now MsgBox "Current date and time is " & currentDateTime
Date Function
Date Function returns the current system date. The following example uses the Date function to show the message for the today’s date.
Dim todayDate todayDate = Date MsgBox "Today date is " & todayDate
Time Function
Time Function returns the current system date.
Day Function
Day Function returns a integer number of the current day in the month between 1 and 31.
The following example shows the day of the month from a specified date.
Dim numDay numDay = Day("May 10, 2012") MsgBox numDay
To get the day number of the today’s date use the following example:
Dim numDay numDay = Day(Date) MsgBox numDay
Month Function
Month function will return a integer number of month in the specified date in between 1 to 12.
Dim numMonth numMonth = Month(Now) MsgBox numMonth
Year Function
Year function returns the whole value of the year. Suppose the if you a date that is ‘May 10, 2012’ than year function will return the ‘2012’.
Dim numYear numYear = Year(Now) MsgBox numYear
DateAdd Function
DateAdd function adds the given time interval to the specified date. You can use the DateAdd fucntion as :
DateAdd(timeinterval, number, date)
Where timeinterval is the string value that is the interval you want to add. You can use the following string expression to add various time interval:
- yyyy – Add Year
- q – Add Quarter
- m – Add Month
- y – Add Day of year
- d – Add Day
- w – Add Weekday
- ww – Add Week of year
- h – Add Hour
- n – Add Minute
- s – Add Second
number is the numeric value of interval you want to add and date is date value to which interval is added.
Example: if you want to add 5 days from now you can write code as:
Dim dt dt = DateAdd("d",5,Now()) MsgBox dt
DateDiff Function
You can calculate the difference of the two dates in number of specified interval. You can use the DateDiff() function as:
DateDiff(interval, date1, date2 [,firstdayofweek] [,firstweekofyear])
interval parameter requires the string expression that is the interval you want to use to calculate the differences between dates. The following is the list of string expressions for the interval parameter:
- yyyy – Add Year
- q – Add Quarter
- m – Add Month
- y – Add Day of year
- d – Add Day
- w – Add Weekday
- ww – Add Week of year
- h – Add Hour
- n – Add Minute
- s – Add Second
date1 and date2 are the two dates you want to use in the calculation of the difference.
There are two optional parameters one is firstdayofweek that specifies the day of the week. If not specified, Sunday is assumed and another is firstweekofyear that specifies the first week of the year. If not specified, the first week is assumed to be the week in which January 1 occurs.
DatePart Function
The DatePart function gives the specified part of a given date and can be used like as:
DatePart(interval,date[,firstdayofweek][,firstweekofyear])
Where interval is the string expression that you want to get the number of the interval and date is the actual date. There are two optional parameters one is firstdayofweek that specifies the day of the week. If not specified, Sunday is assumed and another is firstweekofyear that specifies the first week of the year. If not specified, the first week is assumed to be the week in which January 1 occurs.
firstdayofweek |
|
firstweekofyear |
|
Dim dt dt = CDate("2010-02-16") Dim Quarter, Day, Year Quarter = DatePart("q", dt) Day = DatePart("d", dt) Year = DatePart("yyyy", dt)
DateSerial Function
DateSerial function returns a Variant of subtype Date for a specified year, month, and day and can be used as :
DateSerial(year, month, day)
See the following example:
Dim dt dt = DateSerial(1990, 7, 9) ' Returns July 9, 1990. MsgBox dt
DateValue Function
DateValue function returns a Variant of subtype Date and can be used as:
DateValue(date)
TimeValue Function
TimeValue function returns a Variant of subtype Date containing the time and can be used as:
TimeValue(time)
FormatDateTime Function
The FormatDateTime function returns the date or time according to given argument.
You can use the The FormatDateTime function as:
FormatDateTime(date,format)
where format specifies the date or time format to use:
0 or vbGeneralDate -for the format in case of date: mm/dd/yy and for the time: hh:mm:ss PM/AM.
1 or vbLongDate – for the format weekday, monthname, year
2 or vbShortDate – for the format: mm/dd/yy
3 or vbLongTime – for the format hh:mm:ss PM/AM
4 or vbShortTime -for the format hh:mm