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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12821998/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:33:09  来源:igfitidea点击:

How to generate a UTC Unix Timestamp in C#

c#datetimeunix-timestamp

提问by Paedow

Possible Duplicate:
How to convert UNIX timestamp to DateTime and vice versa?

可能的重复:
如何将 UNIX 时间戳转换为 DateTime,反之亦然?

How can I create a unix timestamp in C#? (e.g. 2012-10-10 14:00:00 -> 1349877600)

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

采纳答案by John Woo

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;
}

回答by aserwin

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

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