Working with Dates in WMI - Converting WMI dates to VBScript Dates
(Page 4 of 5 )
You will also need to be able to convert WMI’s datetime values into VBScript’s Date values. There are two approaches to this. The first is obvious.
strComputer = "."
Set objWMIService = GetObject("winmgmts:" & strComputer & "rootcimv2")
Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOperatingSystem In colOperatingSystems
WScript.Echo DatetimeToDate(objOperatingSystem.InstallDate)
Next
Function DatetimeToDate(strDate)
DatetimeToDate = _
CDate(Mid(strDate, 5, 2) & _
"/" & _
Mid(strDate, 7, 2) & _
"/" & _
Left(strDate, 4) & _
" " & _
Mid (strDate, 9, 2) & _
":" & _
Mid(strDate, 11, 2) & _
":" & _
Mid(strDate, 13, 2))
End Function
This example again returns the date on which the current operating system was installed. This time, the value is being converted to a VT_Date compatible string. Since WMI’s DateTime format is nothing more than a formatted string, VBScript’s string functions can be used to extract the necessary parts. Here, the DatetimeToDate function takes a datetime string such as “20080101000000.000000-300” and converts it to a string like “01/01/2008 00:00:00.” This string is then converted to a VBScript Date value using the CDate function.
Function DatetimeToDate(strUTC)
Set datetime = CreateObject("WbemScripting.SWbemDateTime")
datetime.Value = strUTC
DatetimeToDate = datetime.GetVarDate(True)
End Function
A simpler version of this function could be constructed using the WMI COM object I showed you previously. In this case, the datetime value is used to set the SWbemDateTime object’s Value property directly. Then the GetVarDate method is used to return the VBS-compatible Date. Notice that this function accepts a single Boolean value that indicates whether the returned date is considered local or returned in GMT (adjusted for the offset).
Next: Building Queries with Dates >>
More Windows Scripting Articles
More By Nilpo