Microsoft Windows 2000 Scripting Guide
« Previous
Dates and times play important roles in system administration. For example, when working with event logs you will often want to extract a set of records based on a specific time period (all the events that occurred
VBScript provides several different ways for you to retrieve date and time values. It also provides several
Note
Send us your feedback
|
« Previous Next » |
Microsoft Windows 2000 Scripting Guide
« Previous
The ability to determine the current date or the current time is a useful task in system administration scripting. Many scripts, including those that write to log files or those that write to databases, need to include the current date or time as part of the data written. Scripts that need to take action on specific days or at specific times need to be able to determine the current date and time. Scripts designed to work with a range of dates (for example, retrieve all the error events written to the event log in the past two weeks) need to be able to identify the current date or time to use as a starting point.
VBScript includes three functions that can be used to identify the current date, the current time or both:
For example, the following script retrieves date and time information by using Now, Date, and Time and then displays the results of all the functions in a single message box:
DateInfo = DateInfo & Now & VbCrLf
DateInfo = DateInfo & Date & VbCrLf
DateInfo = DateInfo & Time & VbCrLf
Wscript.Echo DateInfo
When the
Figure 2.12 Date and Time Information by Using Now, Date, and Time Functions
Date and Time Information by Using Now, Date, and Time Functions
Send us your feedback
|
« Previous Next » |
Microsoft Windows 2000 Scripting Guide
« Previous
In working with dates, it is important to know whether a particular value is actually a date or not. This is
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
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 » |