Computing milliseconds since 1970 in C# yields different date than JavaScript

计算从1970毫秒在C #产量比JavaScript不同日期

问题 (Question)

I need to compute the JavaScript getTime method in C#.

For simplicity, I chose a fixed date in UTC and compared the C#:

C#
DateTime e = new DateTime(2011, 12, 31, 0, 0, 0, DateTimeKind.Utc);
DateTime s = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
TimeSpan t = (e - s);
var x = t.TotalMilliseconds.ToString();
=> 1325289600000

and the JavaScript results:

JavaScript
var d = new Date(2011, 12, 31, 0, 0, 0)
var utcDate = new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds());
utcDate.getTime()
=> 1327960800000

Any hints on what I'm doing wrong?

Thanks!

我需要计算的JavaScriptgettime在C #方法。

为简单起见,我选择了在一个固定的日期和时相比,C #:

C#
DateTime e = new DateTime(2011, 12, 31, 0, 0, 0, DateTimeKind.Utc);
DateTime s = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
TimeSpan t = (e - s);
var x = t.TotalMilliseconds.ToString();
=> 1325289600000

和JavaScript的结果:

JavaScript
var d = new Date(2011, 12, 31, 0, 0, 0)
var utcDate = new Date(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate(), d.getUTCHours(), d.getUTCMinutes(), d.getUTCSeconds());
utcDate.getTime()
=> 1327960800000

我在做什么错任何提示?

谢谢!

最佳答案 (Best Answer)

If you meant for the input to be at UTC, you should be doing this instead:

var ts = Date.UTC(2011,11,31,0,0,0);

As SLaks pointed out, months run 0-11, but even then - you must initialize the date as UTC if you want the response in UTC. In your code, you were initializing a local date, and then converting it to UTC. The result would be different depending on the time zone of the computer where the code is running. With Date.UTC, you get back a timestamp - not a Date object, and it will be the same result regardless of where it runs.

From Chrome's debugging console:

Debug Output

This is the same value returned from your .NET code, which looks just fine, except I would return a long, not a string.

如果你想要在UTC时间的输入,你应该这样做而不是:

var ts = Date.UTC(2011,11,31,0,0,0);

作为slaks指出,月运行0-11,但你必须初始化日期,如果你想响应UTC UTC。在你的代码中,你被初始化当地日期,然后转换它以UTC。结果会是不同的时区的计算机上运行的代码。与Date.UTC,你回来的时间戳-不是Date对象,它将同样的结果,无论在哪里,它运行的。

从Chrome的调试控制台:

调试输出

这是相同的值,返回你的。NET代码,它看起来很好,但我会回来的long不,string

答案 (Answer) 2

The date JS is wrong I believe. Omit the var utcDate line and output just d.getTime()

The time between two dates is the same, regardless of timezone and offset. Timezones are relative to an ACTUAL point in time, so whether you call .getTime() on the UTC or EST or PST date, it will be the same relative to 1970-1-1 of the same timezone.

2011-12-31 EST - 1970-1-1 EST 
    == 2011-12-31 PST - 1970-1-1 PST 
    == 2011-12-31 UTC - 1970-1-1 UTC

EDIT: Per @Slaks above, you also are not using the 0-based month (which I also had no idea about).

日期js是错的我相信。省略var utcDate线和输出是d.getTime()

两个日期之间的时间是相同的,无论时区偏移。时区是相对于时间的实际点,所以无论你的电话.getTime()在UTC或EST或PST的日期,这将是相同的相对1970-1-1相同的时区。

2011-12-31 EST - 1970-1-1 EST 
    == 2011-12-31 PST - 1970-1-1 PST 
    == 2011-12-31 UTC - 1970-1-1 UTC

编辑每个“slaks以上,你还没有使用0月(我也不知道)。

答案 (Answer) 3

Javascript months are zero-based.
12 means January of next year.

You want 11.

JavaScript月零基础。
12意味着明年一月。

你想要的11

本文翻译自StackoverFlow,英语好的童鞋可直接参考原文:http://stackoverflow.com/questions/22081128