4

I have last boot time from WMI and it looks as '20141103113859.220250+060'. i want to convert it to number of days and time from the current time.

is it possible?

4 Answers 4

5

From Help

Use the SWbemDateTime object to convert these to regular dates and times. Windows 2000/NT and Windows 98/95: SWbemDateTime is not available. To convert WMI dates to FILETIME or VT_DATE format or to parse the date into component year, month, day, hours, and so on, you must write your own code.

Set dtmInstallDate = CreateObject( _
    "WbemScripting.SWbemDateTime")
strComputer = "."
Set objWMIService = GetObject( _
    "winmgmts:\\" & strComputer & "\root\cimv2")
Set objOS = objWMIService.ExecQuery( _
    "Select * from Win32_OperatingSystem")
For Each strOS in objOS
    dtmInstallDate.Value = strOS.InstallDate
    Wscript.Echo dtmInstallDate.GetVarDate
Next

To get help.

http://msdn.microsoft.com/en-us/windows/hardware/hh852363

Install the Windows SDK but just choose the documentation.

1
  • Just wanted to mention that I confirmed via the MSDN 2003 library that SWbemDateTime is first available on Windows XP and Windows Server 2003. Anything older will not have it. Jul 8, 2021 at 17:15
4

Next simple function should work for any argument in valid CIM_DATETIME format.

Function WMIDateStringToDate(dtmDate)
WMIDateStringToDate = ( Left(dtmDate, 4) _
    & "/" & Mid(dtmDate, 5, 2) _
    & "/" & Mid(dtmDate, 7, 2) _
    & " " & Mid(dtmDate, 9, 2) _
    & ":" & Mid(dtmDate,11, 2) _
    & ":" & Mid(dtmDate,13, 2))
End Function

An example:

InstallDate (wmi): 20141205231553.000000+060
InstallDate:       2014/12/05 23:15:53

However, a wmi query could return Null, e.g. VarType(dtmDate)=1 for a particular instance of a date; in next script is the function modified:

option explicit
Dim strWmiDate
strWmiDate = "20141103113859.220250+060"
Wscript.Echo strWmiDate _
  & vbNewLine & WMIDateStringToDate(strWmiDate) _
  & vbNewLine & DateDiff("d", WMIDateStringToDate(strWmiDate), Now) _ 
  & vbNewLine _
  & vbNewLine & WMIDateStringToDate(Null) _
  & vbNewLine & DateDiff("d", WMIDateStringToDate(Null), Now) 



Function WMIDateStringToDate(byVal dtmDate)
  If VarType(dtmDate)=1 Then
      WMIDateStringToDate = FormatDateTime( Now) 'change to whatever you want
  Else
      '
      ' to keep script locale independent:
      ' returns ANSI (ISO 8601) datetime format (24 h)
      '
      ' yyyy-mm-dd HH:MM:SS
      '
      WMIDateStringToDate = Left(dtmDate, 4) _
          & "-" & Mid(dtmDate, 5, 2) _
          & "-" & Mid(dtmDate, 7, 2) _
          & " " & Mid(dtmDate, 9, 2) _
          & ":" & Mid(dtmDate,11, 2) _
          & ":" & Mid(dtmDate,13, 2)
  End If
End Function

Output:

==>cscript 29535638.vbs
20141103113859.220250+060
2014-11-03 11:38:59
157

09.04.2015 15:36:38
0
1
  • 2
    It's a very good idea to 'to keep script locale independent'. But by returning a string from WMIDateStringToDate (and not a date as the name promised) you have to hope that all calculations/implicit conversions in the calling code succeed for all locales. Use DateSerial + TimeSerial instead. Apr 9, 2015 at 16:48
3

@Serenity has given this same answer while i was writting, but ...

Option Explicit

    WScript.Echo getLastBootUpTime()
    WScript.Echo WMIDate2Date( "20141103113859.220250+060" )
    WScript.Echo GetElapsedTime( getLastBootUpTime(), Now )

Function WMIDate2Date( ByVal WMIDate )
    With WScript.CreateObject("WbemScripting.SWbemDateTime")
        .Value = WMIDate
        WMIDate2Date = .GetVarDate(False)
    End With 
End Function     

Function getLastBootUpTime()
Dim oOS
    For Each oOS In GetObject( "winmgmts:\\.\root\cimv2").ExecQuery("Select LastBootUpTime from Win32_OperatingSystem")
        getLastBootUpTime = WMIDate2Date(oOS.LastBootUpTime)
    Next
End Function

Function GetElapsedTime( ByVal Date1, ByVal Date2 )
Dim seconds, aLabels, aValues, aDividers, i
    aLabels = Array( " days, ", ":", ":", "" )
    aDividers = Array( 86400, 3600, 60, 1 )
    aValues = Array( 0, 0, 0, 0 )
    i = 0
    seconds = Abs( DateDiff( "s", Date1, Date2 ))
    Do While seconds > 0 
        aValues(i) = Fix( seconds / aDividers(i) )
        seconds = seconds - aValues(i) * aDividers(i)
        aValues(i) = CStr(aValues(i)) & aLabels(i)
        i=i+1
    Loop
    GetElapsedTime = Join(aValues, "")
End Function 
2

You won't get around splitting the WMI date string to make it to a date string that VBScript understands. Try this:

<%

    wmiDate  = "20141103113859.220250+060"

    ' note i am using date format: [m/d/Y H:m:s]
    ' if you prefer other format, i.e. [d.m.Y H:m:s] switch mid offsets
    fromDate = Mid(wmiDate,5,2) & "/" & Mid(wmiDate,7,2) & "/" & Left(wmiDate,4)
    fromTime = Mid(wmiDate,9,2) & ":" & Mid(wmiDate,11,2) & ":" & Mid(wmiDate,13,2)

    toDate = Date & " " & Time

    response.write(DateDiff("d",fromDate & " " & fromTime,toDate) & " Days<br />")
    response.write(DateDiff("h",Date & " " & fromTime,toDate) & " Hours<br />")

%>

It uses Mid()and Left()functions to split WMI date into the needed parts for VBScript. Then the DateDiff() function will deliver the interval difference first for d= days and then for h= hours. You will notice when calculating hours i just used the time part of the WMI string, since we already calculated days difference, we only want hours left over.

Interesting article explaining VBScript Date and Time (Iso Formats)

As a comment was so kindly remarking the date format i used and the result of the hour calculation, i added a comment line explaining the date format i used (i used m/d/Y H:m:s but depending on your local, you might prefer d.m.Y H:m:s then you need to swap the Mid() offsets to get the right order). I also appended the current Time to the toDate and in the hour calculation prepended the current Date to calculate the correct time difference.

5
  • With your solution i think DST will not be considered.
    – ro ra
    Apr 9, 2015 at 14:02
  • @rora for timezone one could easily parse the last three digits and subtract them from the minutes '-Mid(wmiDate, 23)' as explained here. So you can get more accurate just with string and date functions without other scripting object. Apr 9, 2015 at 14:15
  • This code is totally useless. The positions for the time parts are wrong, the "h" calculation fails because fromTime has a datepart from 1899. Apr 9, 2015 at 16:23
  • @Ekkehard.Horner thank you for your wording. I just wanted to point out a possible solution and hence provided links for further reading - and yes, fromTime should actually just be added with current date, i apologize, since i just wanted to point out the tools and did not pay too much attention in actual correctness of the hour outcome. Apr 9, 2015 at 16:35
  • @Ekkehard.Horner ah, now i understood, i thought you where commenting the date format itself - thanks for the note - counted wrong there by one offset which is fixed now Apr 9, 2015 at 17:02

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.