如何将Unix时间戳转换为日期时间 [英] how convert unix timestamp to datetime
问题描述
我正在尝试通过以下方式在DateTime中转换此unix时间戳
1415115303410
:
I'm trying to convert this unix timestamp 1415115303410 in DateTime, in this way:
private static DateTime UnixTimeStampToDateTime(long unixTimeStamp)
{
System.DateTime dtDateTime = new DateTime(1970, 1, 1, 0, 0, 0, 0, System.DateTimeKind.Utc);
dtDateTime = dtDateTime.AddMilliseconds(unixTimeStamp);
return dtDateTime;
}
但我输入的日期有误:
日期: {04/11/0045 00:00:00}
But I get a wrong date: Date: {04/11/0045 00:00:00}
注意: dtDateTime.AddSeconds(unixTimeStamp)引发异常。
使用此在线转换工具 http ://www.epochconverter.com/ 我得到了正确的转换:
with this online conversion tool http://www.epochconverter.com/ I get the right conversion:
2014/04/11 15:35:03 GMT + 0 :00
我该如何转换?
推荐答案
您的代码按原样工作正常。这是一个小提琴。
Your code is working just fine, as is. Here is a fiddle.
每个告诉你的人使用 AddSeconds
是错误的。您提供给我们的数字显然以毫秒为单位。一年有31,536,000秒。 1415115303410除以31536000是4487。自1970年1月1日以来已经过去了4,487年。
Everyone that is telling you to use AddSeconds
is wrong. The number you are giving us is clearly in milliseconds. There are 31,536,000 seconds in a year. 1415115303410 divided by 31536000 is 4487. There hasn't been 4,487 years passed since 1/1/1970.
这篇关于如何将Unix时间戳转换为日期时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!