Flylib.com

Books Software

 
 
 

Working with Dates and Times

Microsoft Windows 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous   Next  »   

Working with Dates and Times

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 yesterday , all the events that occurred last week, all the events that occurred last month). To determine such things as service reliability, you need to take the date and time a service started and the date and time the service stopped , and then use the difference to calculate the service uptime. To ensure that your scripts are running as scheduled, you need to log the date and time that a script ran and the date and time that it finished. It is difficult to underestimate the importance of dates and times in managing your computing infrastructure.

VBScript provides several different ways for you to retrieve date and time values. It also provides several methods for performing date arithmetic — that is, calculating such things as the amount of time that elapsed between two events or determining whether the date 180 days from today falls on a weekend .

Note

  • The date and time formats used in VBScript are very different from the date and time formats used in WMI. For information about the WMI date and time formats and how they can be converted to the VBScript format, see "WMI Scripting Primer" in this book.

Microsoft Windows 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous   Next  »   

Retrieving Current Date and Time Values

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:

  • Now — retrieves both the date and the time.
  • Date — retrieves the current date.
  • Time — returns the current time.

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 preceding script runs, a message box similar to the one shown in Figure 2.12 appears.

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


Microsoft Windows 2000 Scripting Guide

microsoft windows 2000 scripting guide

« Previous   Next  »   

Verifying That a Value Is a Date

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:

  • Date literals . These are date values enclosed within pound signs (#). This is the recommended way of using dates in scripts because it eliminates the possibility that VBScript will misinterpret the value as being something other than a date. A date literal might look like this:

    #9/3/2002#

  • Date and time formats recognized by your system settings . For example, if your system is set for English (United States), these values are recognized as valid dates:
    • 6/6/2002
    • 6,1,2002
    • 6-1-2002

    However, this value is not recognized as a valid date:

    • 6.1.2002

    If you change your settings to German (Austria), all four values are recognized as dates.

Note

  • To check valid date formats for your computer, open the Regional and Language Options control panel, click Customize , and then click Date .

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

  • Why is 6/1 a valid date? When using the IsDate function, VBScript tries to construct a plausible date from the value given to it. When it sees a statement that can be interpreted as Month/Day, it automatically appends the current year, resulting in Month/Day/Year. In the preceding script, which was run in the year 2002, that means a value of 6/1/2002, which is a valid date.