Working With Dates and Times in VBScript - Assigning Date values
(Page 4 of 6 )
VBScript provides support for performing calculations on dates and times and provides a number of functions to that end. Before you learn how to perform these calculations, it’s important to understand how to assign date and time values.
dtmValue = #1/31/1976#
dtmValue = #31-1-1976#
dtmValue = #January 31, 1976#
dtmValue = #Jan 31 1976#
dtmValue = #3:01 PM#
You’ve already seen how the Now, Date, and Time functions can be used to assign Date values. It’s important to realize that you can also assign Date values literally using the number symbol in much the same way as a double quotation mark indicates a string. Any acceptable time or date format can be used, and VBScript doesn’t care whether you use a forward slash or a hyphen to separate date parts.
Leaving any portion of the date or time unspecified will assume the current day. For example, assigning January 2 to a date will assume January 2 of the current year.
Be very careful when literally assigning dates using the short-date format. VBScript is smart enough to realize there cannot be 31 months in a year, and it will correctly assign that to the day portion; however, using numbers 12 and below gets confusing. VBScript assumes that the local short-date format is used. Thus, providing the date 1/4/2008 in the United States will assume MM/DD/YYYY format and assign the date January 4, 2008 when you may have intended April 1, 2008 instead.
Assigning an invalid date (such as February 29, 1900—since 1900 was not a leap year) will result in a general syntax error. This can make debugging a bit rough. If you are assigning a date based on user input, it’s a good idea to validate that information to prevent errors.
dtmDate = DateSerial(1976, 1, 31)
dtmTime = TimeSerial(13, 30, 0)
VBScript also provides two functions for creating Date type values based upon integer part values. The DateSerial function returns a Date. It accepts three parameters: a four-digit year, an integer month, and an integer day, respectively. The TimeSerial function works similarly and accepts an integer hour (based on the 24-hour clock), an integer for minutes, and an integer for seconds.
dtmDate = DateValue("January 31, 1976")
dtmDate = CDate("January 31, 1976")
dtmTime = TimeValue("3:30 PM")
dtmTime = CDate("3:30 PM")
VBS provides two more useful functions for assigning Dates. The DateValue and TimeValue both create Date types based on formatted strings. Additionally, the CDate function can be used to convert any sub-type into a Date type. An error will occur if the supplied value cannot be interpreted as a Date.
WScript.Echo CDate("January 31, 1976 3:30 PM")
WScript.Echo DateValue("January 31, 1976 3:30 PM")
WScript.Echo TimeValue("January 31, 1976 3:30 PM")
Output:
1/31/1976 3:30:00 PM
1/31/1976
3:30:00 PM
The reasons for using DateValue and TimeValue to convert a string instead of CDate may not be immediately obvious. Consider the example above. CDate is creating a Date value for the entire supplied string. DateValue and TimeValue will allow you to create Date values containing only the specified portion of the string while ignoring the rest.
Next: Performing calculations >>
More Windows Scripting Articles
More By Nilpo