VBS 中的 EPOCH 日期到正常系统日期

EPOC date to Normal system date in VBS

我读了很多 post 但没有任何帮助。 我的问题看起来很相似,但有点不同。

我必须将 EPOC 时间更改为系统时间格式。

data type = datetime
value= 20160630165419.634204+060

期望的输出 数据类型 = 日期时间

value= 30/06/2016 16:54:19

因为 EPOC 值在点之后有数字并且数据类型是 datetime ,这使得很难在循环中将其除以 10 并获得整数值。 请仅针对给定的输入格式建议解决方案。

第 1 步:使用 DateSerial() and TimeSerial() on numbers (CInt()) extracted from your input (Mid()) 获取子类型 Date

的变体

第 2 步:使用 SetLocale() and Replace() 到 format/stringify 日期

>> SetLocale "de-de"
>> sX = "20160630165419.634204+060"
>> dtD = DateSerial(CInt(Mid(sX, 1, 4)), CInt(Mid(sX, 5, 2)), CInt(Mid(sX, 7, 2)))
>> dtT = TimeSerial(CInt(Mid(sX, 9, 2)), CInt(Mid(sX, 11, 2)), CInt(Mid(sX, 13,2)))
>> dtX = dtD + dtT
>> WScript.Echo dtX, TypeName(dtX)
>> WScript.Echo Replace(dtX, ".", "/")
>>
30.06.2016 16:54:19 Date
30/06/2016 16:54:19

危险,Will Robinson!看看这个测试脚本:

Function WMIDateStringToDate(Mydate)
 WMIDateStringToDate = CDate(Mid(Mydate, 5, 2) & "/" & _
 Mid(Mydate, 7, 2) & "/" & Left(Mydate, 4) _
 & " " & Mid (Mydate, 9, 2) & ":" & _
 Mid(Mydate, 11, 2) & ":" & Mid(Mydate,13, 2))
End Function
'************************************************************
For Each sLocale In Split("de-de en-us")
    SetLocale sLocale
    For Each sDate In Split("20160227235343.000000+060 20160203235343.000000+060")
      On Error Resume Next
       dtX = WMIDateStringToDate(sDate)
       If Err.Number Then dtX = Err.Description
      On Error GoTo 0
       WScript.Echo GetLocale(), sD, sDate, dtX
    Next
Next

及其输出:

cscript 38249865.vbs
1031  20160227235343.000000+060 27.02.2016 23:53:43
1031  20160203235343.000000+060 02.03.2016 23:53:43
1033  20160227235343.000000+060 27.02.2016 23:53:43
1033  20160203235343.000000+060 03.02.2016 23:53:43

查看原因:

The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages. (found here)

您可以使用这个函数 WMIDateStringToDate 来转换您的日期:

WScript.echo WMIDateStringToDate("20160227235343.000000+060")
WScript.echo WMIDateStringToDate("20160630165419.634204+060")
'************************************************************
Function WMIDateStringToDate(Mydate)
 WMIDateStringToDate = CDate(Mid(Mydate, 5, 2) & "/" & _
 Mid(Mydate, 7, 2) & "/" & Left(Mydate, 4) _
 & " " & Mid (Mydate, 9, 2) & ":" & _
 Mid(Mydate, 11, 2) & ":" & Mid(Mydate,13, 2))
End Function
'************************************************************