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

前端 未结 19 2465
抹茶落季
抹茶落季 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

    The latest version of .NET (v4.6) has added built-in support for Unix time conversions. That includes both to and from Unix time represented by either seconds or milliseconds.

    • Unix time in seconds to UTC DateTimeOffset:

    DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeSeconds(1000);
    
    • DateTimeOffset to Unix time in seconds:

    long unixTimeStampInSeconds = dateTimeOffset.ToUnixTimeSeconds();
    
    • Unix time in milliseconds to UTC DateTimeOffset:

    DateTimeOffset dateTimeOffset = DateTimeOffset.FromUnixTimeMilliseconds(1000000);
    
    • DateTimeOffset to Unix time in milliseconds:

    long unixTimeStampInMilliseconds = dateTimeOffset.ToUnixTimeMilliseconds();
    

    Note: These methods convert to and from a UTC DateTimeOffset. To get a DateTime representation simply use the DateTimeOffset.UtcDateTime or DateTimeOffset.LocalDateTime properties:

    DateTime dateTime = dateTimeOffset.UtcDateTime;
    
    0 讨论(0)
  • 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()
    
    0 讨论(0)
  • 2020-11-21 06:52

    To supplement ScottCher's answer, I recently found myself in the annoying scenario of having both seconds and milliseconds UNIX timestamps arbitrarily mixed together in an input data set. The following code seems to handle this well:

    static readonly DateTime UnixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc);
    static readonly double MaxUnixSeconds = (DateTime.MaxValue - UnixEpoch).TotalSeconds;
    
    public static DateTime UnixTimeStampToDateTime(double unixTimeStamp)
    {
       return unixTimeStamp > MaxUnixSeconds
          ? UnixEpoch.AddMilliseconds(unixTimeStamp)
          : UnixEpoch.AddSeconds(unixTimeStamp);
    }
    
    0 讨论(0)
  • 2020-11-21 06:53

    You can use DateTimeOffset.

    For example. I have DateTime object

    var dateTime=new DateTime();
    

    If I want convert the it to Unix time stamps, I can be achieve as follows

    var unixTimeSeconds= new DateTimeOffset(dateTime).ToUnixTimeSeconds()
    

    For more information please visit this link : DateTimeOffset.ToUnixTimeSeconds Method

    0 讨论(0)
  • 2020-11-21 06:53

    A Unix tick is 1 second (if I remember well), and a .NET tick is 100 nanoseconds.

    If you've been encountering problems with nanoseconds, you might want to try using AddTick(10000000 * value).

    0 讨论(0)
  • 2020-11-21 06:55

    I needed to convert a timeval struct (seconds, microseconds) containing UNIX time to DateTime without losing precision and haven't found an answer here so I thought I just might add mine:

    DateTime _epochTime = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
    private DateTime UnixTimeToDateTime(Timeval unixTime)
    {
        return _epochTime.AddTicks(
            unixTime.Seconds * TimeSpan.TicksPerSecond +
            unixTime.Microseconds * TimeSpan.TicksPerMillisecond/1000);
    }
    
    0 讨论(0)
提交回复
热议问题