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

6

15

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

Protrusion answered 13/2, 2010 at 22:26 Comment(0)
H
19

How about:

datediff("s",#1970/1/1#,now())
Heritable answered 13/2, 2010 at 23:5 Comment(2)
Yeah, I changed it to this datediff("s",#1970/1/1#,dateadd("h",5,now()))... not sure what I'll do when DST rolls around though.Maggiemaggio
So there is no way to handle the conversion to UTC automatically?Ceasefire
P
19

This should run faster than the DateDiff solution:

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
Peele answered 18/2, 2010 at 14:35 Comment(0)
Y
1

Here's a solution: 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
Yamen answered 13/2, 2010 at 22:36 Comment(0)
L
1

Here's a solution that returns at UTC because the DateDiff one returns a value based on your time zone:

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

Running it on JScript takes care of the time zone workarounds that would've been necessary in VBA.

Lithesome answered 7/5, 2021 at 19:17 Comment(0)
H
0

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

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
Hujsak answered 10/4, 2018 at 17:45 Comment(0)
A
0

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

Aggappera answered 19/9, 2018 at 12:57 Comment(1)
UnixTimeStamp = DateDiff("s", "01/01/1970 00:00:00", Now()) & Format(Now(), "ms") would give you UnixTimeStamp as 1537362312758Aggappera

© 2022 - 2024 — McMap. All rights reserved.