这个问题在这里已有答案:
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.
我有一个时间戳,我相信是一个unix时间戳,当使用以下转换器时,它正确转换戳记
Value :1365151714493
http://www.epochconverter.com/
我环顾四周,发现example如何将其转换为日期时间对象,方法看起来很简单,创建一个日期时间对象并将日期设置为1970年1月1日的可能之夜并将值添加为第二个:
public static DateTime? ConvertUnixTimeStamp(string unixTimeStamp)
{
return new DateTime(1970, 1, 1, 0, 0).AddSeconds(Convert.ToDouble(unixTimeStamp));
}
问题是每次我用上面的值称这个mehod我得到一个超出范围异常的值 .
我是否需要先对 Value 做任何事情?字符串转换为double ok . 调用 AddSeconds(double)
方法时抛出异常
2 回答
该时间戳(1365151714493)以毫秒为单位,而不是秒 . 您需要除以
1000
或使用AddMilliseconds
代替 . 如果's treated as seconds, it'的日期是将来的某些年份(粗略计算) . 这超出了DateTime
的范围maxes out在10000年,因此抛出ArgumentOutOfRangeException
.您可能还想考虑将其强制为GMT,如V4Vendetta所建议的那样 . 此外,如果您希望混合使用格式(秒或毫秒),则可能需要对解析后的值进行快速大小检查 .
我想你应该尝试一下,因为它与GMT有关
同样从你提到的网站,它假设值是以毫秒为单位,而不是传统的unix timestamp as in seconds