|
|||
|
Date information can be an important part of a script. Perhaps your script receives an order request date from a user. Maybe it collects an appropriate time schedule for some meeting. These tasks would be fairly difficult to perform if a date was simply a string like November 7, 1917 or 06:22:11. Luckily, VBScript provides you with a ready toolkit for working with date and time information. Many of the VBScript functions can work with a date, a time, or a time and date. In VBScript, dates and times are often stored together. The date subtype is viewed as representing both a date and time in its own format. Since all variables in VBScript are variants, date and time values are stored as variants with a subtype of date. The function CDate recognizes date and time literal as well as some numbers that fall within the range of acceptable dates. When converting a number to a date, the whole number portion is converted to a date. Any fractional part of the number is converted to a time of day, starting at midnight. The following example uses the CDate function to convert a string to a date. MyDate = "October 19, 1962" ' Define date. VBDate = CDate(MyDate) ' Convert to Date data type. MyTime = "4:35:47 PM" ' Define time. VBTime = CDate(MyTime)' Convert to Date data type. In general, hard coding dates and times as strings (as shown in this example) is not recommended. Use date and time literal (such as #10/19/1962#, #4:45:23 PM#) instead. VBScript can explicitly convert string to a date or time, using either the DateValue() function for a date, or the TimeValue() function for a time expression. DateValue() function has the following syntax: DateValue(date) The date argument is normally string expression representing a date from January 1, 100 through December 31, 9999. However, date can also be any expression that can represent a date, a time, or both a date and time, in that range. If the date argument includes time information, DateValue doesn't return it. However, if date includes invalid time information (such as "89:98"), an error occurs. If date is a string that includes only numbers separated by valid date separators, DateValue recognizes the order for month, day, and year according to the short date format you specified for your system. DateValue also recognizes unambiguous dates that contain month names, either in long or abbreviated form. For example, in addition to recognizing 12/30/1991 and 12/30/91, DateValue also Go To Page: 1 2
The copyright of the article Working with Dates and Time in VB Script is owned by . Permission to republish Working with Dates and Time in print or online must be granted by the author in writing.
For a complete listing of article comments, questions, and other discussions related to Maxim Karetnikov's VB Script topic, please visit the Discussions page. |
|||
|
|
|||
|
|
|||