【问题标题】:How to get seconds since epoch (1/1/1970) in VBA?如何在 VBA 中获取自纪元(1/1/1970)以来的秒数?
【发布时间】:2011-01-16 14:09:20
【问题描述】:

如何在VBA 中获取自纪元 (1/1/1970) 以来的秒数?

【问题讨论】:

    标签: vba time epoch seconds


    【解决方案1】:

    怎么样:

    datediff("s",#1970/1/1#,now())
    

    【讨论】:

    • 是的,我把它改成了datediff("s",#1970/1/1#,dateadd("h",5,now()))...不过不知道当夏令时到来时我会做什么。
    • 所以没有办法自动处理转UTC?
    【解决方案2】:

    This 应该比 DateDiff 解决方案运行得更快:

    Private Function Long2Date(lngDate As Long) As Date
        Long2Date = lngDate / 86400# + #1/1/1970#
    End Function
    
    Private Function Date2Long(dtmDate As Date) As Long
        Date2Long = (dtmDate - #1/1/1970#) * 86400
    End Function
    

    【讨论】:

      【解决方案3】:

      这里有一个解决方案:http://vbcity.com/forums/t/5084.aspx

      Function UnixTime() As Variant
          'The first parameter determines how the 
          ' difference will be measured in i.e. "S" for seconds
          UnixTime = DateDiff("S", "1/1/1970", Now())
      End Function
      

      【讨论】:

        【解决方案4】:

        我尝试为我的时区修改它并且 考虑夏令时。时区更改时可以有不同的设置。

        Function Epoch2Date(lngDate As Long) As Date
        'transfer to date
            Epoch2Date = lngDate / 86400# + #1/1/1970#
        'check if it is summer time
            If IsDST(Epoch2Date) = False Then
        'here you can use diferent values depend on time zone
            Epoch2Date = Epoch2Date - 0.041666667
            Else
            Epoch2Date = Epoch2Date - 0.0833333333333333
            End If
        End Function
        
        Public Function IsDST(ByVal d0 As Date) As Boolean
           IsDST = d0 >= NextSun("24.3." & Year(d0) & " 01:59:59") And d0 < NextSun("24.10." & Year(d0) & " 01:59:59")
        End Function
        
        Private Function NextSun(d1 As Date) As Date
                'if 24.3 or 24.10 is sunday returns 31.3 or 31.10
                If Weekday(d1, vbMonday) = 7 Then
                    NextSun = d1 + 7
                Else
                'if not return nearest sunday
                    NextSun = d1 + 7 - Weekday(d1, vbMonday)
                End If
            End Function
        

        【讨论】:

          【解决方案5】:

          DateDiff("s", "01/01/1970 00:00:00", Now()) & Format(Now(), "ms")

          【讨论】:

          • UnixTimeStamp = DateDiff("s", "01/01/1970 00:00:00", Now()) & Format(Now(), "ms") 会给你 UnixTimeStamp 为 1537362312758
          【解决方案6】:

          这是一个在 UTC 返回的解决方案,因为 DateDiff 会根据您的时区返回一个值:

          Private Function UnixEpoch() As Long
          
              Set ScriptEngine = CreateObject("MSScriptControl.ScriptControl")
              ScriptEngine.Language = "JScript"
              
              ScriptEngine.AddCode "function unixTime() { var d = new Date(); var s = Math.round(d.getTime() / 1000); return s; } "
              
              UnixEpoch = ScriptEngine.Run("unixTime")
          
          End Function
          

          在 JScript 上运行它会处理 VBA 中必需的时区变通办法。

          【讨论】:

            猜你喜欢
            • 2011-08-06
            • 1970-01-01
            • 2012-12-11
            • 1970-01-01
            • 2012-02-19
            • 1970-01-01
            • 2012-10-07
            • 2010-11-08
            相关资源
            最近更新 更多