Javascript新Date函数中的奇怪行为 [英] Strange behavior in Javascript new Date function

查看:35
本文介绍了Javascript新Date函数中的奇怪行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用javascript中的字符串作为Date对象,但是我在这里看到javascript解析日期字符串很奇怪.

I tried to make Date object from string in javascript, but i see javascript parse date string very strange here.

> new Date("2012-01-01");
Sun Jan 01 2012 07:00:00 GMT+0700 (ICT)

> new Date("01-01-2012");
Sun Jan 01 2012 00:00:00 GMT+0700 (ICT)

> new Date("2012-01-01") == new Date("01-01-2012")
false

我使用的是Chrome 32,您会看到它们相差7个小时.请告诉我这里发生了什么事?

I use Chrome 32, as you can see they are 7 hour differ. Please tell me what happend here?

推荐答案

这与浏览器如何实现 Date.parse ( spec ),然后首先尝试将字符串与已知格式进行匹配,以确定哪些值在何处.我希望不同的浏览器以略有不同的方式实现该决策树,但是Chrome的实现可能假设"2012-01-01" 版本是),而" 01-01-2012"版本是基于您当地的时区的本地化版本,无需理会它("01-01-2012 00:00:00" ),因此7个小时的差异是基于ISO标准日期和本地化日期之间的7个小时的偏移量. Date.prototype.toString()(规格),相比之下,它应该显示本地时间,并由 Date 构造函数返回,这就是为什么它在测试的两个返回值中都进行了本地化的原因.

It's all about how the browser implements Date.parse (spec). That method is called on the string passed into the Date constructor (spec) and first tries to match the string against a known format to determine what values are where. I'd expect different browsers implemented that decision tree in slightly different ways, but Chrome's implementation probably assumes that the "2012-01-01" version is a prefix to the ISO-8601 standard which is based on Zulu, or GMT/UTC, and includes a timezone ("2012-01-01T00:00:00Z-07:00") while the "01-01-2012" version is a localization based on your LOCAL time zone, and doesn't bother to specify it ("01-01-2012 00:00:00") so the 7 hour difference is based on the 7 hour offset between the ISO standard date and the localized date. Date.prototype.toString() (spec), by contrast, is supposed to display local time and is returned by the Date constructor, which is why it's localized in both return values from your test.

规范中获取 Date.parse :

该函数首先尝试根据日期时间字符串格式( 15.9.1.15 ).如果字符串不符合该格式,则该函数可能会退回到任何特定于实现的试探法或特定于实现的日期格式.不可识别的字符串或包含字符串格式的非法元素值的日期将导致Date.parse返回NaN.

The function first attempts to parse the format of the String according to the rules called out in Date Time String Format (15.9.1.15). If the String does not conform to that format the function may fall back to any implementation-specific heuristics or implementation-specific date formats. Unrecognizable Strings or dates containing illegal element values in the format String shall cause Date.parse to return NaN.

表示不使用 15.9.1.15 ,浏览器可以随时进行编排,也可以只给您 NaN .尽管这是标准,但某些浏览器因实际上没有遵循标准而臭名昭著,因此您可以考虑通过自己解析数据并使用其他 Date 构造函数(规范).

Meaning if you don't use the full ISO-8601 date specified in 15.9.1.15, the browser can make it up as it goes along or just give you NaN. Even though this IS the standard, some browsers are notorious for not actually FOLLOWING standards, so you might consider just specifying all of the parameters unambiguously by parsing the data yourself and using the other Date constructor (spec).

这篇关于Javascript新Date函数中的奇怪行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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