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

发布于 2024-11-03 19:02:56 字数 319 浏览 1 评论 0原文

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

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

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

My client receives a spreadsheet with a number of columns, one being a "date". Only the date turns out to be formatted as Date(1292291582263-0700) (a JSON date it seems).

I need to convert and work with this JSON date in MM/DD/YYYY format, elsewhere in this spreadsheet's code (VBA).

Does anyone know how to parse and convert this JSON date format into a MM/DD/YYYY format? I have read lots of solutions on SO that are in Javascript, C#, or ASP.NET, etc but all I have to work with is Excel 2010 and VBA code for this project. Is there way to arrive at a readable format as I need?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

晚雾 2024-11-10 19:02:56

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

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 

Millisecond Epoch time with a +/- offset?

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 
不顾 2024-11-10 19:02:56

此函数处理 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

This function take care of date before 1971-01-01 and some problem with overflow.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文