【问题标题】:Excel VBA: Convert a date string to a Unix timestampExcel VBA:将日期字符串转换为 Unix 时间戳
【发布时间】:2012-09-01 19:12:23
【问题描述】:

Excel VBA:如何转换日期字符串
“2012-08-20”到时间戳:1345438800
我需要将 1345438800 作为长数据类型值存储在单元格中。

【问题讨论】:

    标签: excel vba date timestamp


    【解决方案1】:

    为确保准确完整的往返行程,请在 DateDiff 和 DateAdd 函数中添加时间戳:

    日期到时间戳:

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

    迄今为止的时间戳:

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

    【讨论】:

      【解决方案2】:

      日期到时间戳:

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

      迄今为止的时间戳:

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

      【讨论】:

      • @xeo 不接受回答您的问题的答案被认为是粗鲁的。到目前为止,您已经提出了几个问题,得到了很好的答案,但没有一个接受。
      • ...并创建一个 UNIX 时间戳:DateDiff("s", "1/1/1970", Now)
      猜你喜欢
      • 1970-01-01
      • 2011-03-16
      • 1970-01-01
      • 2019-07-30
      • 2012-06-23
      • 2011-09-23
      • 1970-01-01
      • 2022-01-21
      相关资源
      最近更新 更多