Excel VBA: Convert a date string to a Unix timestamp

后端 未结 2 559
温柔的废话
温柔的废话 2020-12-09 23:25

Excel VBA: How to convert date string
\"2012-08-20\" to timestamp: 1345438800
I need to store 1345438800 in cell as long data type value.

2条回答
  •  旧时难觅i
    2020-12-09 23:58

    To ensure an accurate complete round trip, add a timestamp to the DateDiff and DateAdd functions:

    Date to timestamp:

    Public Function toUnix(dt) As Long
        toUnix = DateDiff("s", "1/1/1970 00:00:00", dt)
    End Function
    

    Timestamp to date:

    Public Function fromUnix(ts) As Date
        fromUnix = DateAdd("s", ts, "1/1/1970 00:00:00")
    End Function
    

提交回复
热议问题