ro ra
ro ra

Reputation: 419

vbscript: how to convert a date into days and time

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?

Upvotes: 5

Views: 2840

Answers (4)

hexerei software
hexerei software

Reputation: 3160

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.

Upvotes: 2

JosefZ
JosefZ

Reputation: 30113

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

Upvotes: 5

Serenity
Serenity

Reputation: 184

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.

Upvotes: 6

MC ND
MC ND

Reputation: 70923

@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 

Upvotes: 4

Related Questions