どのようにVBAでのエポック(1970年1月1日)からの秒を取得しますか?

StackOverflow https://stackoverflow.com/questions/2259324

  •  20-09-2019
  •  | 
  •  

質問

どのように私は VBA の中で(1970年1月1日)エポックからの秒数を得ることができますか?

役に立ちましたか?

解決

いかがます:

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

他のヒント

このには、より高速な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

ここでは解決策があります: 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

私は自分のタイムゾーンのためにそれを修正しようと試みたと アカウントのDSTに取ります。それが変化したときにタイムゾーンがdiferentの設定を持つことができます。

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

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top