JavaScript的Date如何工作? [英] How does JavaScript's Date work?

查看:39
本文介绍了JavaScript的Date如何工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以向我解释输出 http://jsfiddle.net/mark69_fnd/NhuLe/产生的结果?

Can someone explain me the output http://jsfiddle.net/mark69_fnd/NhuLe/ produces?

new Date('2012-07-01') == Sat Jun 30 2012 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-07-09') == Sun Jul 08 2012 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-07-10') == Mon Jul 09 2012 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-07-31') == Mon Jul 30 2012 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-08-1') == Wed Aug 01 2012 00:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-08-9') == Thu Aug 09 2012 00:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-08-10') == Thu Aug 09 2012 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-08-31') == Thu Aug 30 2012 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-09-1') == Sat Sep 01 2012 00:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-09-9') == Sun Sep 09 2012 00:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-09-10') == Sun Sep 09 2012 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2012-12-09') == Sat Dec 08 2012 19:00:00 GMT-0500 (Eastern Standard Time)
new Date('2012-12-31') == Sun Dec 30 2012 19:00:00 GMT-0500 (Eastern Standard Time)
new Date('2013-01-01') == Mon Dec 31 2012 19:00:00 GMT-0500 (Eastern Standard Time)
new Date('2013-01-09') == Tue Jan 08 2013 19:00:00 GMT-0500 (Eastern Standard Time)
new Date('2013-02-09') == Fri Feb 08 2013 19:00:00 GMT-0500 (Eastern Standard Time)
new Date('2013-03-09') == Fri Mar 08 2013 19:00:00 GMT-0500 (Eastern Standard Time)
new Date('2013-04-09') == Mon Apr 08 2013 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2013-05-09') == Wed May 08 2013 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2013-06-09') == Sat Jun 08 2013 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2013-07-09') == Mon Jul 08 2013 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2013-08-09') == Thu Aug 08 2013 20:00:00 GMT-0400 (Eastern Daylight Time)
new Date('2013-09-09') == Sun Sep 08 2013 20:00:00 GMT-0400 (Eastern Daylight Time)

我有兴趣了解它如何决定计算日期.请注意 2012-07-09 2012-08-9 2013-08-09 之间的区别.

I am interested to understand how it decides to compute the day. Please, note the difference between 2012-07-09, 2012-08-9 and 2013-08-09.

我在Chrome上运行了它.

I ran it on Chrome.

推荐答案

这是一个非常有趣且微妙的问题.

This is a very interesting, and subtle, question.

原因是您的某些日期采用的是规范中定义的类似于ISO-8601的格式,因此被解析为GMT,而其他日期则不是,因此它们依赖于非标准日期解析,它似乎是(在Chrome中)使用本地时间.

The reason is that some of your dates are in the ISO-8601-like format defined in the specification, and so are parsed as GMT, but others are not, and so they fall back on non-standard date parsing, which appears to be (in Chrome) using local time instead.

日期字符串 2012-07-01 符合

The date string 2012-07-01 conforms to the format specified in Section 15.9.1.15, and so according to the rules of that section, it is parsed in timezone Z (GMT). Then you output it and it's output in local time, four hours or so earlier, and so the date changes as the original value (having no time part) is at midnight.

日期字符串 2012-08-1 不符合该格式(它在 1 <前需要 0 /code>).这将我们带出指定行为的领域. Date 构造函数在提供字符串时,遵循与 Date.parse 相同的规则,该规则在

The date string 2012-08-1 does not conform to that format (it needs a 0 before the 1). This takes us out of the land of specified behavior. The Date constructor, when given a string, follows the same rules as Date.parse, which are defined in Section 15.9.4.2, which says amongst other things:

该函数首先尝试根据日期时间字符串格式(15.9.1.15)中调用的规则来解析字符串的格式.如果字符串不符合该格式,则该函数可能会退回到任何特定于实现的启发式或特定于实现的日期格式.

(我的重点)

这个故事的寓意是:坚持指定的格式.:-)

The moral of this story is: Stick to specified formats. :-)

但需要注意的一点是:规范中定义的日期/时间格式相对较新(大约在三年前的ES5开始).在此之前,没有没有定义的日期/时间格式需要解析 Date 构造函数(或 Date ).它只需要能够解析出任何 Date#toString ,但是那是特定于实现的.而且,较旧的浏览器确实无法解析 2012-08-01 .尽管未指定,但几乎所有浏览器(我测试过的每个浏览器)都会 解析 2012/08/01 .当然,现在我想回头看看它们使用的时区(并检查它们是否都使用相同的时区)...

But a side note on that: The date/time format defined in the spec is relatively new (as of ES5, about three years ago). Prior to that, there was no defined date/time format that the Date constructor (or Date parse) was required to parse. It just had to be able to parse whatever Date#toString spat out, but what that was was implementation-specific. And older browsers will indeed fail to parse 2012-08-01. Although it's not specified, nearly all browsers (every one I've ever tested) will parse 2012/08/01, though. Of course, now I want to go back and see what time zone they use (and check whether they all use the same one)...

这篇关于JavaScript的Date如何工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆