Export (0) Print
Expand All

Converting WMI Dates to a Standard Date-Time Format

Microsoft® Windows® 2000 Scripting Guide

Although UTC dates are intimidating at first glance, they are relatively easy to convert to a standard date-time format. This is because 1) VBScript treats UTC dates as strings, meaning that they can be manipulated using VBScript string functions and 2) UTC dates use a standard fixed-width format. The year will always take up the first four character positions in a UTC string, the month will always take up the next two character positions, and so forth. These character positions are described in Table 6.10.

Table 6.10 Character Positions of a UTC Date-Time Value

Character Positions

Description

Sample Value

14

Four digits representing the year (such as 2001 or 2002).

2002

56

Two digits representing the month. For example, January is represented by the digits 01; November, by the digits 11.

10

78

Two digits representing the day of the month. For example, the 5th day is represented by the digits 05; the 23rd day by the digits 23.

18

914

Six zeros representing the hours, minutes, and seconds of the day (in 24-hour format). If you prefer, you can specify values other than 0 to create more finely targeted searches. For example, to search for folders created after 1:47 P.M. on a given day, set these characters to 134700, where 13 represents the hours (1:00 P.M. in 24-hour format), 47 represents the minutes, and 00 represents the seconds.

000000

15

A period (.).

.

1621

Six zeros representing the milliseconds.

000000

2225

The number of minutes difference between your local time and Greenwich mean time.

480

To convert a UTC date to a standard date-time format, you simply select the desired date-time components (such a month, day, and year) and construct a date-time string of your own. For example, with the UTC value 20020710113047.000000420, you need to:

  1. Extract the month (07).

  2. Extract the day (10).

  3. Extract the year (2002).

  4. Combine them into a standard date format: 07/10/2002.

You can extract the individual date components using VBScript functions such as Left and Mid. (These functions are explained in more detail in "VBScript Primer" in this book.) For example, to extract the day (character positions 7 and 8), you use code similar to this, in which dtmInstallDate is a variable used to represent the date being converted:



Mid(dtmInstallDate, 7 ,2)

VBScript interprets this line of code as follows: Take the string dtmInstallDate, start in the seventh character position, and return two values (characters 7 and 8). In the date 20020710113047.000000420, characters 7 and 8 are 10, the tenth day of the month.

The following function converts a UTC date to a standard date by:

  1. Extracting the month.

  2. Appending a backslash (/).

  3. Extracting the day.

  4. Appending a backslash.

  5. Extracting the year.

  6. Adding a space.

  7. Extracting the hour.

  8. Appending a colon (:).

  9. Extracting the minutes.

  10. Appending a colon.

  11. Extracting the seconds.

  12. Converting the resultant string to a date using the CDate function.



WMIDateStringToDate = CDate(Mid(dtmInstallDate, 5, 2) & "/" & _
     Mid(dtmInstallDate, 7, 2) & "/" & Left(dtmInstallDate, 4) _
         & " " & Mid (dtmInstallDate, 9, 2) & ":" & _
             Mid(dtmInstallDate, 11, 2) & ":" & Mid(dtmInstallDate, _
                 13, 2))

If passed the UTC value 20020219145216.000000480, the function returns this date:



2/19/02 2:52:16 PM

The script shown in Listing 6.19 retrieves the date that the operating system was installed on a computer. This value is not echoed in UTC format; instead, it is passed to a function named WMIDateStringToDate. This function converts the UTC value to a standard date-time format. This standard date is then echoed to the screen.

Listing 6.19 Converting a UTC Value to a Standard Date-Time Value

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15


strComputer = "."
Set objSWbemServices = GetObject("winmgmts:\\" & strComputer & "\root\cimv2")
Set objOS = objSWbemServices.ExecQuery("SELECT * FROM Win32_OperatingSystem")
For Each strOS in objOS
 dtmInstallDate = strOS.InstallDate
 strReturn = WMIDateStringToDate(dtmInstallDate)
 Wscript.Echo strReturn
Next
Function WMIDateStringToDate(dtmInstallDate)
 WMIDateStringToDate = CDate(Mid(dtmInstallDate, 5, 2) & "/" & _
 Mid(dtmInstallDate, 7, 2) & "/" & Left(dtmInstallDate, 4) _
 & " " & Mid (dtmInstallDate, 9, 2) & ":" & _
 Mid(dtmInstallDate, 11, 2) & ":" & Mid(dtmInstallDate, _
 13, 2))
End Function

Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2014 Microsoft