C# 将纪元/unix 转换为日期时间

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15926261/
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 18:30:09  来源:igfitidea点击:

Convert epoch/unix to Datetime

c#datetimeunix-timestamp

提问by Andy Clark

This is question is not a duplicate, this quesitons demonstrates a problem with a method of conversion, not how to perform the conversion. Read the question in full.

这个问题不是重复的,这个问题展示了转换方法的问题,而不是如何执行转换。完整阅读问题。

I have a timestamp which I believe is a unix time stamp, when using the following converter it correctly converts the stamp

我有一个时间戳,我认为它是一个 unix 时间戳,当使用以下转换器时,它会正确转换时间戳

Value: 1365151714493

值:1365151714493

http://www.epochconverter.com/

http://www.epochconverter.com/

I have looked around and found an exampleon how to convert this to a datetime obect and the method seems simple, create a datetime object and set the date to the might night on 1/1/1970 and add the value as second:

我环顾四周,发现了一个例子就如何将其转换为datetime obect和方法看似简单,创建一个DateTime对象,并设置日期威力晚上1/1/1970,并添加值作为第二:

public static DateTime? ConvertUnixTimeStamp(string unixTimeStamp)
{
    return new DateTime(1970, 1, 1, 0, 0).AddSeconds(Convert.ToDouble(unixTimeStamp));
}

The problem is everytime I call this mehod with the value above I get a value out of range exception.

问题是每次我用上面的值调用这个方法时,我都会得到一个超出范围的值异常。

Do I need to do anything with the value first? the string converts to a double ok. the exception is thrown when calling the AddSeconds(double)methos

我需要先对价值做任何事情吗?字符串转换为双重确定。调用时抛出异常AddSeconds(double)methos

采纳答案by Chris Sinclair

That timestamp (1365151714493) is in milliseconds, not seconds. You'll need to divide by 1000or use AddMillisecondsinstead. If it's treated as seconds, it's a date some 43,259 (rough calculation) years in the future. This is beyond the range of DateTimewhich maxes outat the year 10000, thus throwing the ArgumentOutOfRangeException.

该时间戳 (1365151714493) 以毫秒为单位,而不是秒。您需要除以1000或使用AddMilliseconds来代替。如果将其视为秒,则它是未来大约 43,259(粗略计算)年的日期。这是超出了范围DateTime,其马克塞斯在一年10000,从而投掷ArgumentOutOfRangeException

public static DateTime? ConvertUnixTimeStamp(string unixTimeStamp)
{
    return new DateTime(1970, 1, 1, 0, 0, 0).AddMilliseconds(Convert.ToDouble(unixTimeStamp));
}

You may also want to consider forcing it to GMT as V4Vendetta suggested. In addition, if you expect to have a mix of formats (seconds OR milliseconds) perhaps a quick size check on the parsed value might be prudent.

您可能还需要考虑按照 V4Vendetta 的建议将其强制为 GMT。此外,如果您希望有多种格式(秒或毫秒),那么对解析值进行快速大小检查可能是谨慎的。

回答by V4Vendetta

I guess you should try it as since it is with respect to GMT

我想您应该尝试一下,因为它与格林威治标准时间有关

Also from the site you mention it assumes that the value is in milliseconds and not the traditional unix timestamp as in seconds

同样从您提到的站点中,它假定该值以毫秒为单位,而不是以秒为单位的传统unix 时间戳

DateTime epoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
epoch = epoch.AddMilliseconds(yourvalue);// your case results to 4/5/2013 8:48:34 AM