admin管理员组文章数量:1401669
I am calculating TimeStamp
in javascript
using following code
var timestamp = new Date;
that gives me result 1436504446858
contain 13
digits depend upon current time.
The same thing calculated in C#
using following code
DateTime centuryBegin = new DateTime(1970, 1, 1);
DateTime currentDate = DateTime.Now;
long elapsedTicks = currentDate.Ticks - centuryBegin.Ticks;
Here i got result 14365252465308044
that contains 17
digits
Why the two methods gives so much of difference ?
I am calculating TimeStamp
in javascript
using following code
var timestamp = new Date;
that gives me result 1436504446858
contain 13
digits depend upon current time.
The same thing calculated in C#
using following code
DateTime centuryBegin = new DateTime(1970, 1, 1);
DateTime currentDate = DateTime.Now;
long elapsedTicks = currentDate.Ticks - centuryBegin.Ticks;
Here i got result 14365252465308044
that contains 17
digits
Why the two methods gives so much of difference ?
Share Improve this question asked Jul 10, 2015 at 5:19 Rajeev KumarRajeev Kumar 4,9639 gold badges52 silver badges88 bronze badges1 Answer
Reset to default 9Why the two methods gives so much of difference ?
The Javascript code is giving you the number of milliseconds since the Unix epoch.
The .NET code (if you'd got it right - more on that in a second) is giving you the number of ticks since the Unix epoch. There are 10 million ticks per second, so 10,000 ticks per millisecond. That's why you're getting 4 more digits.
Now, the other problem is that your .NET code is using the local time - whereas it should reflect the UTC time, given that you're finding the number of ticks (or milliseconds) since the Unix epoch, which is midnight January 1st in 1970 UTC. So you want:
DateTime unixEpoch = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
DateTime utcNow = DateTime.UtcNow;
TimeSpan elapsedTime = utcNow - unixEpoch;
double millis = elapsedTime.TotalMilliseconds;
Note how if you represent the difference in time as a TimeSpan
, you can convert it to the units you're interested in. (Although a TimeSpan
is just "a length of time" which doesn't know a start/end, so you can't use it to find a difference in variable-length units such as years and months.)
本文标签: Timestamp calculated in Javascript and C are differentStack Overflow
版权声明:本文标题:Timestamp calculated in Javascript and C# are different - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744317589a2600339.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论