Dot-Net

.net 中的 DateTime 到 UnixTime Stamp

  • June 5, 2020

vb dot net 中是否有任何函式可以將日期時間轉換為 unix 時間戳

任何幫助表示讚賞

{Edit} 刪除了舊的參考連結

自 1970 年 1 月 1 日以來的秒數 = unix 時間

要在 VB.NET 中獲得此功能,請參見下面的範例(例如使用 DateTime.UtcNow,但您可以在此處插入任何所需的 DateTime)

Dim uTime As Double
uTime = (DateTime.UtcNow - New DateTime(1970, 1, 1, 0, 0, 0)).TotalSeconds

我在某個時候寫了這些 - 都與上麵類似,只是在英國這里為我提供了關於舊夏季時間調整的更多細節。

Public Function UnixToTime(ByVal strUnixTime As String) As Date
   UnixToTime = DateAdd(DateInterval.Second, Val(strUnixTime), #1/1/1970#)
   If UnixToTime.IsDaylightSavingTime = True Then
       UnixToTime = DateAdd(DateInterval.Hour, 1, UnixToTime)
   End If
End Function

Public Function TimeToUnix(ByVal dteDate As Date) As String
   If dteDate.IsDaylightSavingTime = True Then
       dteDate = DateAdd(DateInterval.Hour, -1, dteDate)
   End If
   TimeToUnix = DateDiff(DateInterval.Second, #1/1/1970#, dteDate)
End Function

引用自:https://stackoverflow.com/questions/1942519