javascript Date.parse 和 Date.UTC 给出不同的结果

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11875094/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-26 14:37:37  来源:igfitidea点击:

javascript Date.parse and Date.UTC give different results

javascriptdate

提问by Yang

Can anyone explain to me why?

谁能向我解释为什么?

d = Date.parse('8/15/2012 '+'11:59:45 AM');
alert(d);
alert(Date.UTC(2012, 7, 15, 11, 59, 45));

?

?

回答by Corbin

Date.parse assumes local time if not specified.

如果未指定,则 Date.parse 假定为本地时间。

The UTC one, however, is obviously UTC.

然而,UTC 显然是 UTC。

For example, my computer is UTC -5 (well, Chicago CDT actually), so the two timestamps happen to be 5 hours apart for me.

例如,我的计算机是 UTC -5(嗯,实际上是芝加哥 CDT),所以两个时间戳对我来说恰好相隔 5 小时。

You will get the same thing if you specify UTC:

如果您指定 UTC,您将得到同样的结果:

Date.parse('8/15/2012 '+'11:59:45 AM UTC'); //1345031985000
Date.UTC(2012, 7, 15, 11, 59, 45); //1345031985000

回答by Euric

I'll assume the difference in months was a typo in your question.

我假设几个月的差异是您问题中的错字。

Date.parse returns the difference between the date provided and midnight 1 January 1970.

Date.parse 返回提供的日期与 1970 年 1 月 1 日午夜之间的差值。

Date.UTC returns the difference between your date and midnight 1 January 1970 GMT.

Date.UTC 返回您的日期与格林威治标准时间1970 年 1 月 1 日午夜之间的差值。

If your timezone is set to GMT (UTC) you should expect to see the same value returned by both calls.

如果您的时区设置为 GMT (UTC),您应该会看到两个调用返回的相同值。