Working with Dates in WMI - Converting VBScript dates to WMI Dates
(Page 3 of 5 )
VBScript dates must first be converted to WMI’s datetime format if you wish to use them in a query. This can be done easily by building a function that serves this purpose. However, WMI’s COM interface provides a class specifically for working with date and time conversions.
dtmDate = #January 1, 2008#
Set dateTime = CreateObject("WbemScripting.SWbemDateTime")
dateTime.SetVarDate(dtmDate, True)
WScript.Echo "CIM datetime " & dateTime
WScript.Echo "Local datetime " & dateTime.GetVarDate()
The WbemScripting object’s SWbemDateTime class returns a date or time as a datetime value. By creating an instance of this object class, you can easily prepare dates and times for use in WMI queries. Since the SWbemDateTime class’s default method is Value, a reference to the object will return the datetime value without specifying the actual Value property.
CIM datetime 20080101000000.000000-300
Local datetime 1/1/2008
The SetVarDate method is used to set the datetime value by providing a VBScript date. It accepts a VBScript Date value as its parameter. A second, optional parameter is a Boolean value that indicates whether the time is a local time or GMT time. This is the easiest way to get a date from VBScript into WMI datetime format. You are not, however, limited to this conversion. The SWbemDateTime class has several other useful methods as well for returning date and time portions.
dtmDate = Now()
Set dateTime = CreateObject("WbemScripting.SWbemDateTime")
dateTime.SetVarDate(Now)
WScript.Echo "Year:" & dateTime.Year
WScript.Echo "Month:" & dateTime.Month
WScript.Echo "Day:" & dateTime.Day
WScript.Echo "Hours:" & dateTime.Hours
WScript.Echo "Minutes:" & dateTime.Minutes
WScript.Echo "Seconds:" & dateTime.Seconds
WScript.Echo "Microseconds:" & dateTime.Microseconds
WScript.Echo "UTC Offset:" & dateTime.UTC
The example above enumerates each of the properties that the SWbemDateTime class provides for returning date and time portions. These are self explanatory.
Year:2008
Month:9
Day:10
Hours:23
Minutes:48
Seconds:22
Microseconds:0
UTC Offset:-240
Here you see that the UTC offset is -240. Earlier, on this same system the offset was -300. What does that mean? The Eastern Time Zone in the United States observes Daylight Savings Time. Each spring our clock moves ahead one hour, reducing the UTC offset by 60 minutes. Thus, the current offset is only -240 when daylight savings time is being observed.
Next: Converting WMI dates to VBScript Dates >>
More Windows Scripting Articles
More By Nilpo