Export (0) Print
Expand All

Converting a Standard Date to a WMI Date-Time Format

Microsoft® Windows® 2000 Scripting Guide

As noted previously, you cannot use standard date-time formats - such as 10/18/2002 - when writing WMI queries. Instead, you need to convert any dates used in your queries to UTC format. This requires two steps: 1) You must determine the offset (difference in minutes) between your time zone and Greenwich Mean Time, and 2) you must convert 10/18/2002 to a UTC value.

Determining the Offset from Greenwich Mean Time

Admittedly, WMI makes it difficult to work with dates and times; fortunately, WMI at least makes it easy to determine the offset between your time zone and Greenwich Mean Time. The WMI class Win32_TimeZone includes a property - Bias - that returns the GMT offset. The script in Listing 6.20 shows how this property is retrieved.

Listing 6.20 Determining the Offset from Greenwich Mean Time

  
1
2
3
4
5
6
7
8


strComputer = "."
Set objSWbemServices = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colTimeZone = objSWbemServices.ExecQuery _
 ("SELECT * FROM Win32_TimeZone")
For Each objTimeZone in colTimeZone
 Wscript.Echo "Offset: "& objTimeZone.Bias
Next

When the preceding script runs under CScript on a computer operating on Pacific daylight time, the following value is echoed to the command window:



Offset: -480

Converting a Date to a UTC Value

After you determine the GMT offset, you must then convert a standard date such as 10/18/2002 to a UTC date. To convert a standard date to a UTC date, you can use VBScript date functions such as Year, Month, and Day to isolate the individual components that make up a UTC date. (See Table 6.10 for details.) After you have individual values for these components, you can concatenate them in the same manner as you would any other string value.

UTC dates are treated as strings because the GMT offset must be appended to the end. If the date were seen as a number, this value:



20011018113047.000000-480

Would be erroneously treated as a mathematical equation (parentheses added for clarity):



(20011018113047.000000) - (480)

For example, in the date 10/18/2002, the individual components are:

  • Year: 2002

  • Month: 10

  • Day: 18

The script would need to combine these three values, the string "113047.000000" (representing the time, including milliseconds), and the GMT offset to derive a UTC date. For example, (parentheses again added for clarity):



(2002) & (10) & (18) & (113047.000000) & (-480)

Note

  • You can use the VBScript functions Hour, Minute, and Second to convert the time portion of a UTC date. Thus, a time such as 11:30:47 A.M. would be converted to 113047.

There is one complicating factor. The month must take up positions 5 and 6 in the string; the day must take up positions 7 and 8. This is no problem with month 10 and day 18. But how do you get July 5 (month 7, day 5) to fill up the requisite positions?

The answer is to add a leading zero to each value, thus changing the 7 to 07 and the 5 to 05. To do this, use the VBScript Len function to check the length (number of characters) in the month and the day. If the length is 1 (meaning that there is just one character), add a leading zero. Thus:



If Len(dtmMonth) = 1 Then
    dtmMonth = "0" & dtmMonth
End If

The script shown in Listing 6.21 converts the current date to a UTC date. The script first determines the GMT offset and then converts the date to UTC format. When appending the time value (000000.000000), the script uses the function CStr; this ensures that the value is appended as a string and not as a number.

Listing 6.21 Converting the Current Date to a UTC Date

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26


strComputer = "."
Set objSWbemServices = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colTimeZone = objSWbemServices.ExecQuery _
 ("SELECT * FROM Win32_TimeZone")
For Each objTimeZone in colTimeZone
 strBias = objTimeZone.Bias
Next

dtmCurrentDate = Date
dtmTargetDate = Year(dtmCurrentDate)

dtmMonth = Month(dtmCurrentDate)
If Len(dtmMonth) = 1 Then
 dtmMonth = "0" & dtmMonth
End If

dtmTargetDate = dtmTargetDate & dtmMonth

dtmDay = Day(dtmCurrentDate)
If Len(dtmDay) = 1 Then
 dtmDay = "0" & dtmDay
End If

dtmTargetDate = dtmTargetDate & dtmDay & "000000.000000"
dtmTargetDate = dtmTargetDate & Cstr(strBias)

The script in Listing 6.22 demonstrates a more practical use of these conversions. The script determines the GMT offset, and then converts a specified current date (in this case, 10/18/2002) to UTC date-time format. After the date has been converted, that value is used to search a computer and returns a list of all the folders that were created after 10/18/2002.

Listing 6.22 Retrieving Folders Based on Creation Date

  
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33


strComputer = "."
Set objSWbemServices = GetObject("winmgmts:" _
 & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colTimeZone = objSWbemServices.ExecQuery _
 ("SELECT * FROM Win32_TimeZone")
For Each objTimeZone in colTimeZone
 strBias = objTimeZone.Bias
Next

dtmCurrentDate = "10/18/2002"
dtmTargetDate = Year(dtmCurrentDate)

dtmMonth = Month(dtmCurrentDate)
If Len(dtmMonth) = 1 Then
 dtmMonth = "0" & dtmMonth
End If

dtmTargetDate = dtmTargetDate & dtmMonth

dtmDay = Day(dtmCurrentDate)
If Len(dtmDay) = 1 Then
 dtmDay = "0" & dtmDay
End If

dtmTargetDate = dtmTargetDate & dtmDay & "000000.000000"
dtmTargetDate = dtmTargetDate & Cstr(strBias)

Set colFolders = objSWbemServices.ExecQuery _
 ("SELECT * FROM Win32_Directory WHERE CreationDate < '" & _
 dtmtargetDate & "'")
For Each objFolder in colFolders
 Wscript.Echo objFolder.Name
Next

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