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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2259324/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 10:16:47  来源:igfitidea点击:

How to get seconds since epoch (1/1/1970) in VBA?

vbatimeepochseconds

提问by aF.

How can I get seconds since epoch (1/1/1970) in VBA?

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

回答by Fionnuala

How about:

怎么样:

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

回答by Oorang

Thisshould run faster than the DateDiff solution:

应该比 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

回答by aF.

Here's a solution: http://vbcity.com/forums/t/5084.aspx

这是一个解决方案: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

回答by Lynx

I tryed modify it for my timezone and take into account DST. Timezones can have diferent settings when it changes.

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

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

回答by JOAN

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

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