.net 中的日期时间到 UnixTime Stamp

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1942519/
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-03 13:42:08  来源:igfitidea点击:

DateTime to UnixTime Stamp in .net

.netvb.netdatetimeunix-timestamp

提问by Ramji

Is there any function in vb dot net to convert datetime to unix time stamp If I google I get only the vice versa but not vb.net to unix time stamp

vb dot net 中是否有任何函数可以将日期时间转换为 unix 时间戳

Any help is appreciated

任何帮助表示赞赏

回答by curtisk

{Edit} removed old reference link

{Edit} 移除了旧的参考链接

Seconds since Jan 1, 1970 = unix time

自 1970 年 1 月 1 日以来的秒数 = unix 时间

To get this in VB.NET see below example (in example using DateTime.UtcNow, but you can plug in any DateTime you want there)

要在 VB.NET 中获得它,请参见下面的示例(在使用 DateTime.UtcNow 的示例中,但您可以插入您想要的任何 DateTime)

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

回答by Ian

I wrote these at some point - all similar to the above just with a little more detail on the old summer time adjustment thing for me here in the UK.

我在某个时候写了这些 - 所有类似于上面的内容,只是对我在英国这里的旧夏令时调整事情有更多的细节。

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

回答by John Boker

Looking at http://www.codeproject.com/KB/cs/timestamp.aspxit shows unix->.net so you can go backwards most likely:

查看http://www.codeproject.com/KB/cs/timestamp.aspx它显示 unix->.net 所以你很可能可以倒退:

DateTime dt = "the date";
DateTime start= new System.DateTime(1970, 1, 1, 0, 0, 0, 0);

TimeSpan ts = (dt - start);

ts.TotalSeconds //unix timestamp

or something similar will do it.

或类似的东西会做到这一点。

note, this has not been tested by me so it probably wont work :)

请注意,这尚未经过我的测试,因此可能无法正常工作:)

回答by Chris Tyler

In javascript, at least, unix timestamps are milliseconds, and I found a case where I needed them. This code makes use of .Net using 10,000 ticks / millisecond. Here's the code I used:

至少在 javascript 中,unix 时间戳是毫秒,我发现我需要它们的情况。此代码使用 .Net 使用 10,000 滴答/毫秒。这是我使用的代码:

Public Module UnixTime
    Const UnixEraStartTicks As Long = 621355968000000000
    <Extension> Public Function UnixTimestamp(value As Date) As Long
        Dim UnixEraTicks = value.Ticks - UnixEraStartTicks
        Return UnixEraTicks \ 10000
    End Function
    Public Function DateFromUnix(timestamp As Long) As Date
        Return New Date(UnixEraStartTicks + timestamp * 10000)
    End Function
End Module