Microsoft® Windows® 2000 Scripting Guide
« Previous | Next »
In working with dates, it is important to know whether a particular value is actually a date or not. This is especially true when making WMI queries or when working with databases; your script will fail if it attempts to use an invalid date in these situations.
The IsDate function can tell you whether a supplied value is a date. IsDate returns False (0) if the value is not a date and True ( 1) if the value is a date. Date values can be passed using either of the following:
#9/3/2002#
However, this value is not recognized as a valid date:
If you change your settings to German (Austria), all four values are recognized as dates.
Note
The following script creates an array of values and then enumerates each item in the array. The script then uses IsDate to determine whether the item represents a valid date and echoes the value and a message indicating that this is actually a date.
DateArray = Array(
"6/1/2002
",
"June 1, 2002
",
"6
",
"6/1
") For Each dtmDate in DateArray If IsDate(dtmDate) = 0 Then Wscript.Echo dtmDate &
"is not a valid date.
"Else Wscript.Echo dtmDate &
"is a valid date.
"End If Next
When the preceding script runs under CScript, the following information appears in the command window:
6/1/2002 is a valid date. June 1, 2002 is a valid date. 6 is not a valid date. 6/1 is a valid date.
Note
Send us your feedback | « Previous | Next » |