How can I convert a Unix timestamp to DateTime and vice versa?

前端 未结 19 2459
抹茶落季
抹茶落季 2020-11-21 06:37

There is this example code, but then it starts talking about millisecond / nanosecond problems.

The same question is on MSDN, Seconds since the Unix epoch in C#<

19条回答
  •  遇见更好的自我
    2020-11-21 06:51

    Unix time conversion is new in .NET Framework 4.6.

    You can now more easily convert date and time values to or from .NET Framework types and Unix time. This can be necessary, for example, when converting time values between a JavaScript client and .NET server. The following APIs have been added to the DateTimeOffset structure:

    static DateTimeOffset FromUnixTimeSeconds(long seconds)
    static DateTimeOffset FromUnixTimeMilliseconds(long milliseconds)
    long DateTimeOffset.ToUnixTimeSeconds()
    long DateTimeOffset.ToUnixTimeMilliseconds()
    

提交回复
热议问题