如何在C#中生成UTC Unix时间戳

时间:2012-10-10 14:36:44

标签: c# date time unix-timestamp

  

可能重复:
  How to convert UNIX timestamp to DateTime and vice versa?

如何在C#中创建unix时间戳? (例如2012-10-10 14:00:00 - > 1349877600)

2 个答案:

答案 0 :(得分:37)

private double ConvertToTimestamp(DateTime value)
{
    //create Timespan by subtracting the value provided from
    //the Unix Epoch
    TimeSpan span = (value - new DateTime(1970, 1, 1, 0, 0, 0, 0).ToLocalTime());

    //return the total seconds (which is a UNIX timestamp)
    return (double)span.TotalSeconds;
}

答案 1 :(得分:-1)

DateTime.UtcNow - new DateTime(2012,10,10,14,0,0)).TotalSeconds

相关问题