| << D.3.0- Math Functions | AppendixD | D.4.1- Unsupported Date Functions and Statements >> |
Date and Time Functions and Statements
There are a number of ways to display and represent dates and times. This includes date literals, which are valid date expression, enclosed in number signs (#). You need to be careful when using date literals because VBScript only lets you use the US-English date format, mm/dd/yyyy. This is true even if a different locale is being used on the machine. This might lead to problems when trying to use date literals in other formats, because in most cases the date will be accepted although converted to a different date. #10/12/1997# will be interpreted as October 12, 1997, but you might in fact want December 10, 1997, because your locale settings interprets dates as dd/mm/yyyy. Date literals only accept the forward slash (/) as the date separator.
The data range for a date is January 1, 100 to December 31, 9999, both inclusive. Internally, dates are stored as part of real numbers or to be more specific as a Variant subtype Double (5). The digits to the left of the decimal separator represent the date and the digits to the right of the decimal separator represent the time. Negative numbers are used internally for representing dates prior to December 30, 1899.
Below is a list of functions used for converting and formatting dates and times.
CDate |
Returns an expression converted to Variant subtype Date (7).
|
|
Syntax
|
CDate(date)
date is any valid date expression.
|
|
Note
|
CDate is internationally aware, which means that the return value is based on the locale settings on the machine. Dates and times will be formatted with the appropriate time and date separators, and for dates the correct order of year, month and day are applied. Date and time literals are recognized.
|
|
Example |
Dim dtmValue dtmValue = CDate( #12/10/1997#)
|
|
See Also |
IsDate |
|
Date |
Returns a Variant subtype Date (7) indicating the current system date. |
|
Syntax |
Date |
|
Example
|
MsgBox Date
|
|
See Also |
Now and Time |
|
DateAdd |
Adds or subtracts a time interval to a specified date and returns the new date.
|
|
Syntax
|
DateAdd(interval, number, date)
interval can have these
values: d Day h Hour m Month n Minute s Second
|
|
|
w Weekday ww Week of year y Day of year yyyy Year
number is a numeric expression that must be positive if you want to add or negative if you want to subtract.
date must be a Variant or date literal to which interval is added.
|
|
Note
|
DateAdd is internationally aware, which means that the return value is based on the locale settings on the machine. Dates and times will be formatted with the appropriate time and date separators and for dates the correct order of year, month and day are applied. An error occurs if the date returned precedes the year 100.
|
|
Example |
MsgBox DateAdd("m", 3, "1-Jan-99")
|
|
See Also |
DateDiff, DatePart |
|
DateDiff |
Returns the interval between two dates.
|
|
Syntax
|
DateDiff(interval, date1, date2, [firstdayofweek], [firstweekofyear])
interval can have these
values: d Day h Hour m Month n Minute s Second w Weekday ww Week of year y Day of year yyyy Year
date1 and date2 are date expressions.
firstdayofweek (Optional)
specifies the first day of the week. Use one of the following constants: |
|
|
|
|
Note |
|
|
Example |
MsgBox DateDiff("yyyy", #11-22-1967#, Now)
|
|
See Also |
DateAdd, DatePart |
|
DatePart |
Returns a specified part of a date.
|
|
Syntax
|
DatePart(interval, date, [firstdayofweek], [firstweekofyear])
interval can have these
values: d Day h Hour m Month n Minute s Second w Weekday ww Week of year y Day of year yyyy Year
date is a date expression. |
|
|
firstdayofweek (Optional)
specifies the first day of the week. Use one of the following constants:
|
|
Example
|
MsgBox DatePart("ww", Now, vbMonday, vbFirstFourDays)
|
|
See Also |
DateAdd, DateDiff |
|
DateSerial |
Returns a Variant subtype Date (7) for the specified year, month and day.
|
|
Syntax
|
DateSerial(year, month, day)
year is an expression that evaluates to a number between 0 and 9999. Values between 0 and 99, both inclusive, are interpreted as the years 1900 – 1999.
month is an expression that must evaluate to a number between 1 and 12.
|
|
Note
|
|
|
|
|
Example |
MsgBox DateSerial( 1999, 07, 29)
|
|
See Also |
Date, DateValue, Day, Month, Now, TimeSerial, TimeValue, Weekday and Year |
|
DateValue |
Returns a Variant subtype Date (7).
|
|
Syntax
|
DateValue(date)
date is an expression representing a date, a time, or both, in the range January 1, 100 – December 31, 9999. |
|
Note
|
|
|
Example |
DateValue("07/29/1999")
|
|
See Also |
Date, DateSerial, Day, Month, Now, TimeSerial, TimeValue, Weekday and Year |
|
Day |
|
|
Syntax
|
Day(date)
date is any valid date expression.
|
|
Note
|
|
|
Example |
MsgBox Day("July 29, 1999")
|
|
See Also |
Date, Hour, Minute, Month, Now, Second, Weekday and Year |
|
FormatDateTime |
See under String functions |
|
Hour |
|
|
Syntax
|
Hour(time)
time is any valid time expression.
|
|
Note
|
|
|
Example |
MsgBox Hour("12:05:12")
|
|
See Also |
Date, Day, Minute, Month, Now, Second, Weekday and Year |
|
IsDate |
Returns a Variant subtype Boolean (11) indicating whether an
|
|
Syntax
|
IsDate(expression)
expression is any expression you want to evaluate as a date or time.
|
|
Example
|
MsgBox IsDate(Now)
' true
|
|
See Also |
CDate, IsArray, IsEmpty, IsNull, IsNumeric, IsObject and VarType |
|
Minute |
Returns a number between 0 and
59, both inclusive, indicating the
|
|
Syntax
|
Minute(time)
time is any valid time expression.
|
|
Note
|
|
|
Example |
MsgBox Minute("12:45")
|
|
See Also |
Date, Day, Hour, Month, Now, Second, Weekday and Year
|
|
Month |
Returns a number between 1 and
12, both inclusive, indicating the
|
|
Syntax
|
Month(date)
date is any valid date expression.
|
|
Note
|
|
|
Example |
MsgBox Month(#7/29/1999#)
The MsgBox will display 7.
|
|
See Also |
Date, Day, Hour, Minute, Now, Second, Weekday and Year |
|
MonthName |
Returns a Variant subtype String (8) for the specified month.
|
|
Syntax
|
MonthName(month, [abbreviate])
month is a number between 1 and 12 for each month of the year beginning with January.
abbreviate (Optional) is a boolean value indicating if the month name should be abbreviated or spelled out (default)
|
|
Note
|
|
|
Example |
MsgBox MonthName(2) '
February
|
|
See Also |
WeekdayName |
|
Now |
Returns the system's current date and time.
|
|
Syntax
|
Now
|
|
Example |
Dim dtmValue
dtmValue now holds the current system date and time.
|
|
See Also |
Date, Day, Hour, Month, Minute, Second, Weekday and Year |
|
Second |
Returns a Variant subtype Date (7) indicating the number of seconds (0-59) in the specified time.
|
|
Syntax
|
Second(time)
time is any valid time expression.
|
|
Note
|
|
|
Example |
MsgBox Second("12:45:56")
The MsgBox will display 56.
|
|
See Also |
Date, Day, Hour, Minute, Month, Now, Weekday and Year |
|
Time |
Returns a Variant subtype Date (7) indicating the current system time.
|
|
Syntax
|
Time
|
|
Example |
Dim dtmValue
|
|
See Also |
Date, Now |
|
Timer |
Returns a Variant subtype Single (5) indicating the number of seconds that have elapsed since midnight. This means that it is "reset" every 24 hours.
|
|
Syntax
|
Timer
|
|
Example |
Dim dtmStart, dtmStop
|
|
TimeSerial |
Returns a Variant subtype Date (7) for the specified hour, minute and second.
|
|
Syntax
|
TimeSerial(hour, minute, second)
hour is an expression that evaluates to a number between 0 and 23.
minute is an expression that must evaluate to a number between 0 and 59.
|
|
Note
|
|
|
Example |
MsgBox TimeSerial(23, 07, 29)
|
|
See Also |
Date, DateSerial, DateValue, Day, Month, Now, TimeValue, Weekday and Year |
|
TimeValue |
Returns a Variant subtype Date (7) containing the time.
|
|
Syntax
|
TimeValue(time)
time is an expression in the range 0:00:00 – 23:59:59.
|
|
Note
|
|
|
Example |
TimeValue("23:59")
|
|
See Also |
Date, DateSerial, DateValue, Day, Month, Now, TimeSerial, Weekday and Year |
|
Weekday |
Returns a number indicating the day of the week.
|
|
Syntax
|
Weekday(date, [firstdayofweek])
date is any valid date expression.
firstdayofweek (Optional) specifies the first day of the week. Use one of the following constants:
vbUseSystemDayOfWeek 0 (Use NLS API setting) vbSunday 1 (Default) vbMonday 2 vbTuesday 3 vbWednesday 4 vbThursday 5 vbFriday 6 vbSaturday 7
|
|
Note
|
vbTuesday 3 vbWednesday 4 vbThursday 5 vbFriday 6 |
|
Example |
Weekday(#July 29, 1999#)
|
|
See Also |
Date, Day, Month, Now and Year |
|
WeekdayName |
Returns a Variant subtype String (8) for the specified weekday.
|
|
Syntax
|
WeekdayName(weekday, [abbreviate], [firstdayofweek])
weekday is a number between 1 and 7 for each day of the week. This value depends on the firstdayofweek setting.
abbreviate (Optional) is
a boolean value indicating if the weekday name should be abbreviated or
spelled out firstdayofweek (Optional) is a numeric value indicating the first day of the week. Use one of the following constants:
|

RSS