将JSON日期转换为MM / DD / YYYY格式?

时间:2011-04-27 14:53:46

标签: json vba date excel-2010

我的客户收到一个包含多个列的电子表格,其中一列是“日期”。只有日期的格式为Date(1292291582263-0700)(似乎是JSON日期)。

我需要转换并使用此电子表格代码(VBA)中其他位置的MM / DD / YYYY格式的JSON日期。

有谁知道如何解析这个JSON日期格式并将其转换为MM / DD / YYYY格式?我已经阅读了大量有关Javascript,C#或ASP.NET等的解决方案,但我必须使用的是Excel 2010和此项目的VBA代码。有没有办法按照我的需要达到可读格式?

2 个答案:

答案 0 :(得分:6)

具有+/-偏移的毫秒纪元时间?

Const test = "1292291582263-0700"

Dim dt As String: dt = Left$(test, 13)
Dim off As String: off = Mid$(test, 14)

Dim d As Date: d = DateAdd("s", CCur(dt) / 1000, "01/01/1970")
Debug.Print d
<<< 14/12/2010 01:53:02 

d = DateAdd("h", Left$(off, 3), d)
d = DateAdd("n", Right$(off, 2), d)
Debug.Print d
<<< 13/12/2010 18:53:02 

答案 1 :(得分:1)

此函数可以处理1971-01-01之前的日期以及溢出问题。

Public Function Convert_Microsoft_Json_Date_To_Date(strMicrosoftDate As String) As Date
'Convert_Microsoft_Json_Date_To_Date("/Date(-2208970800000-0530)/") => 1900-01-01
'Convert_Microsoft_Json_Date_To_Date("/Date(2208970800000-0530)/") => 2039-12-31 14:00:00
Dim strProcedureName As String: strProcedureName = "Convert_Microsoft_Json_Date_To_Date"
Dim lngDateNumber As Long
Dim strOffsetSign As String
Dim strOffsetHours As String
Dim strOffsetMinutes As String
Dim dteDateNoOffset As Date
Dim dteRealDate As Date
Dim curSecondToAdd As Currency '+ or -
Dim curSecondLeft As Currency
Dim curSecondMax As Currency
Dim IsOffsetExist As Boolean

On Error GoTo err_

strMicrosoftDate = Replace(strMicrosoftDate, "/", "")
strMicrosoftDate = Replace(strMicrosoftDate, "(", "")
strMicrosoftDate = Replace(strMicrosoftDate, ")", "")
strMicrosoftDate = Replace(strMicrosoftDate, "Date", "")

strOffsetSign = Left(Right(strMicrosoftDate, 5), 1)
strOffsetHours = Left(Right(strMicrosoftDate, 4), 2)
strOffsetMinutes = Right(strMicrosoftDate, 2)

IsOffsetExist = strOffsetSign = "+" Or strOffsetSign = "-"

If IsOffsetExist Then
    'Remove the offset part if exist
    strMicrosoftDate = Left(strMicrosoftDate, Len(strMicrosoftDate) - 5)
End If
curSecondMax = 1000000000# 'if curSecondToAdd is to high we get overflow, and I do it in 2 step below to get my date
curSecondToAdd = CCur(strMicrosoftDate) / 1000 'Convert miliseconds to seconds

If Abs(curSecondToAdd) > curSecondMax Then
    If curSecondToAdd >= 0 Then
        dteDateNoOffset = DateAdd("s", curSecondToAdd - curSecondMax, DateSerial(1970, 1, 1))
        dteDateNoOffset = DateAdd("s", curSecondMax, dteDateNoOffset)
    Else
        dteDateNoOffset = DateAdd("s", curSecondToAdd + curSecondMax, DateSerial(1970, 1, 1))
        dteDateNoOffset = DateAdd("s", -curSecondMax, dteDateNoOffset)
    End If
Else

    dteDateNoOffset = DateAdd("s", curSecondToAdd, DateSerial(1970, 1, 1))
End If

'Debug.Print "Date no offset: " & dteDateNoOffset

If IsOffsetExist Then
    dteRealDate = DateAdd("h", CInt(strOffsetSign & strOffsetHours), dteDateNoOffset)
    dteRealDate = DateAdd("n", CInt(strOffsetSign & strOffsetMinutes), dteRealDate)
Else
    dteRealDate = dteDateNoOffset
End If
'Debug.Print "Date real: " & dteRealDate

Convert_Microsoft_Json_Date_To_Date = dteRealDate

err_exit:

    Exit Function


err_:
Select Case Err.Number

    Case Else
        MsgBox Err.Description & " | " & Err.Number & vbCrLf & "Procedure: " & strProcedureName & IIf(Erl <> 0, vbCrLf & "Ligne: " & Erl, ""), vbCritical
        Resume err_exit
        Resume
End Select
End Function