将刻度转换为DateTime [英] Converting ticks to DateTime
问题描述
这个网站上有很多问题解释了如何做到这一点。我的问题我在做什么似乎为所有其他人工作,我没有得到正确的日期或时间。代码是...
There are a number of questions on this site explaining how to do this. My problem I when I do what seems to work for everyone else I don't get the correct date or time. The code is ...
long numberOfTicks = Convert.ToInt64(callAttribute);
startDateTime = new DateTime(numberOfTicks);
callAttribute
的值为=1379953111
The value of callAttribute
is = "1379953111"
转换成 numberOfTicks
= 1379953111
After converting it the value of numberOfTicks
= 1379953111
但是 DateTime
最终是 startDateTime
= {1/1/0001 12 :02:17 AM}
我已经采取了相同的价值,并在线转换,并提出正确的日期/时间。
I have taken the same value for ticks and converted it online and it comes up with the correct date/time.
我做错了什么?
推荐答案
似乎是一些蜱虫;我怀疑这是一个UNIX时间戳(从1970/01/01 UTC开始的秒数)
Your value doesn't seem to be a number of ticks; I suspect it's a UNIX timestamp (number of seconds since 1970/01/01 UTC)
这是一个从UNIX时间戳转换的功能:
Here's a function to convert from a UNIX timestamp:
static readonly DateTime _unixEpoch =
new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
public static DateTime DateFromTimestamp(long timestamp)
{
return _unixEpoch.AddSeconds(timestamp);
}
这篇关于将刻度转换为DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!