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

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

提交回复
热议问题