vbscript:如何将日期转换为日期和时间

时间:2015-04-09 10:11:20

标签: vbscript

我从WMI获得了最后的启动时间,它看起来像是20141103113859.220250 + 060'。我想将它转换为当前时间的天数和时间。

有可能吗?

4 个答案:

答案 0 :(得分:4)

来自帮助

  

使用SWbemDateTime对象将这些对象转换为常规日期和时间。   Windows 2000 / NT和Windows 98/95:SWbemDateTime不可用。要将WMI日期转换为FILETIME或VT_DATE格式或将日期解析为组件年,月,日,小时等,您必须编写自己的代码。

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

获得帮助。

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

安装Windows SDK,但只需选择文档。

答案 1 :(得分:3)

下一个简单函数应该适用于有效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

一个例子:

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

但是,wmi查询可能会返回Null,例如VarType(dtmDate)=1表示日期的特定实例;在下一个脚本中修改了函数:

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

输出:

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

09.04.2015 15:36:38
0

答案 2 :(得分:1)

您不会绕过WMI日期字符串,使其成为VBScript理解的日期字符串。试试这个:

<%

    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 />")

%>

它使用Mid()Left()函数将WMI日期拆分为VBScript所需的部分。然后,DateDiff()函数将首先为d =天提供区间差异,然后为h =小时提供区间差异。您会在计算小时时注意到我刚刚使用了WMI字符串的时间部分,因为我们已经计算了天数差异,我们只需要剩余时间。

有趣的文章解释VBScript Date and Time (Iso Formats)

由于评论是如此友好地评论我使用的日期格式和小时计算的结果,我添加了一条注释行来解释我使用的日期格式(我使用{{1但是,根据您的本地情况,您可能更喜欢m/d/Y H:m:s,然后您需要交换d.m.Y H:m:s偏移以获得正确的顺序)。我还将当前Mid()附加到Time,并在小时计算中添加当前toDate以计算正确的时差。

答案 3 :(得分:1)

@Serenity在我写作的时候给出了同样的答案,但是......

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