vb.net 在 VB .NET 中将 DateTime 转换为秒数

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

Converting DateTime to number of seconds in VB .NET

vb.netdatetimetimedatetime-conversion

提问by GVillani82

For converting number of seconds to DateTime, in VB .NET, I use the following code:

为了将秒数转换为 DateTime,在 VB .NET 中,我使用以下代码:

    Dim startDate As New DateTime(1970, 1, 1)
    Dim targetDate As DateTime
    Dim noOfSeconds As Integer = DataInSeconds

    targetDate = startDate.AddSeconds(noOfSeconds)

where DataInSeconds is an Integer containing the number of seconds (starting from 1/1/1970)

其中 DataInSeconds 是一个包含秒数的整数(从 1/1/1970 开始)

This works good. But I don't know how make the inverse conversion. (from DateTime to number of seconds). Anyone can help me?

这很好用。但我不知道如何进行逆转换。(从日期时间到秒数)。任何人都可以帮助我吗?

回答by Oded

When you subtract DateTimeinstances from each other, you get a TimeSpan- you can use this to get the number of seconds:

当您DateTime从彼此中减去实例时,您会得到一个TimeSpan- 您可以使用它来获取秒数:

Dim startDate As New DateTime(1970, 1, 1)
Dim noOfSeconds As Integer

noOfSeconds = (currentDate - startDate).TotalSeconds

回答by Hans Passant

1/1/1970 is the Unix epoch. Beware that it is a UTC date, you cannot ignore that in conversions. Thus:

1/1/1970 是 Unix 纪元。请注意,这是一个 UTC 日期,您不能在转换中忽略它。因此:

Module DateConversion
    Public ReadOnly Property Epoch() As DateTime
        Get
            Return New DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc)
        End Get
    End Property

    Public Function FromUnix(ByVal seconds As Integer, local As Boolean) As DateTime
        Dim dt = Epoch.AddSeconds(seconds)
        If local Then dt = dt.ToLocalTime
        Return dt
    End Function

    Public Function ToUnix(ByVal dt As DateTime) As Integer
        If dt.Kind = DateTimeKind.Local Then dt = dt.ToUniversalTime
        Return CInt((dt - Epoch).TotalSeconds)
    End Function
End Module

Watch out for ToUnix(), the DateTimeKind may be unspecified, as it was in your snippet. Consider using DateTimeOffset instead to make it unambiguous. And be sure to do something reasonable in 2038 when all of this comes tumbling down.

注意 ToUnix(),DateTimeKind 可能未指定,因为它在您的代码段中。考虑改用 DateTimeOffset 以使其明确。并且一定要在 2038 年做一些合理的事情,当所有这一切都崩溃时。

回答by Enumerator

Label1.Text = New DateTime(1970, 1, 1, 0, 0, 0, 
    DateTimeKind.Utc).AddSeconds(CLng(TextBox1.Text) / 1000)      

Make a textbox and a button and a label, put this code into the button and depending if you're using microseconds (keep the /1000) or seconds (delete the /1000) will show you the date/time etc.

制作一个文本框、一个按钮和一个标签,将此代码放入按钮中,并根据您使用的是微秒(保留 /1000)还是秒(删除 /1000)来显示日期/时间等。

回答by Kris Wiloch

Public Function Date2Unix(ByVal vDate As Date) As Long
 Return (vDate - #1970/01/01#).TotalSeconds
End Function

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
 MsgBox(Date2Unix(Now()))
End Sub