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

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

    var dt = DateTime.Now; 
    var unixTime = ((DateTimeOffset)dt).ToUnixTimeSeconds();
    

    // 1510396991

    var dt = DateTimeOffset.FromUnixTimeSeconds(1510396991);
    

    // [11.11.2017 10:43:11 +00:00]

提交回复
热议问题