Javascript Date.parse错误时以虚线划分并以年开始 [英] Javascript Date.parse bug when dash-delimited and starts with year

查看:113
本文介绍了Javascript Date.parse错误时以虚线划分并以年开始的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果这是一个真正的文档和/或Javascript的Date.parse方法的实现错误,请求确认。

Am seeking confirmation if this is a bona fide documentation and/or implementation bug with Javascript's Date.parse method.

我所指的文档是< a href =https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/parse =noreferrer> https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/日期/解析,他们会说'如果您没有指定时区,则会假定本地时区'。

The docs I'm referring to are at https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/parse and they say 'If you do not specify a time zone, the local time zone is assumed.'

但以下代码显示,尽管没有指定时区,但是假定本地时间不是(但是正在应用我的时区偏移量),如果传递给Date.parse的字符串以4位数年份表示形式开头,并且虚线划分。

But the following code shows that, despite not specifying a time zone, local time is not being assumed (but rather my timezone offset is being applied), if the string passed to Date.parse begins with the 4-digit year representation, and is dash-delimited.

var euroStyleDate = '2011-10-04';
var amerStyleDate = '10/04/2011';
var euroStyleParsed = Date.parse(euroStyleDate);
var amerStyleParsed = Date.parse(amerStyleDate);

console.log(euroStyleParsed); //1317686400000
console.log(amerStyleParsed); //1317700800000

console.log(new Date(euroStyleParsed)); 
//Date {Mon Oct 03 2011 20:00:00 GMT-0400 (Eastern Daylight Time)}
console.log(new Date(amerStyleParsed)); 
//Date {Tue Oct 04 2011 00:00:00 GMT-0400 (Eastern Daylight Time)}

甚至可能有其他情况,如果我不正确,我相信我不是第一个发现这个案件。所以,除了确认之外,如果有人知道相关的链接,我一定会喜欢指出更深入的信息。

There may even be other cases, and I'm sure I'm not the first to discover this if I am incorrect. So beyond confirmation, I'd surely love to be pointed at more in-depth information on this if anybody knows of pertinent links.

我在FF3中遇到这样的情况, Chrome for Windows,当然只是特别的IE8甚至似乎无法在2011-10-04执行转换:我只是在我的应用程序中获得一个空字符串

I'm experiencing this in FF3, Chrome for Windows and of course just to be special IE8 doesn't even seem to able to perform the conversion on 2011-10-04 whatsoever: I'm just getting an empty string in my application

提前感谢任何进一步的洞察力或资源。

Thanks in advance for any further insight or resources.

推荐答案

* 更新: *看起来这里有几个不同的标准:

*Update:*It looks like there are several different standards at work here:


  1. EMCAScript< 5个标准允许在标准IETF格式中使用日期。 2010年10月03日。有了这些日期,假定当地时区。

  1. The EMCAScript < 5 standard allowed for dates in the standard IETF format, e.g. Sun Oct 03 2010. With these dates, the local timezone is assumed.

在ECMAScript 5中,限制版本的ISO 8601标准也被允许 2010-10-03 。该规范似乎说(可能遵循ISO 8601?),在这种情况下,如果没有指定UTC时区,则假定。

In ECMAScript 5, a limited version of the ISO 8601 standard is also allowed, e.g. 2010-10-03. The spec seems to say (perhaps following ISO 8601?) that in this case, the UTC timezone is assumed if one is not specified.

我没有发现一个规范说 Date.parse 可以处理 mm / dd / yyyy 日期,但我的浏览器(Chrome 14)显然可以,也可能是其他浏览器也可以。假设当地时区,这似乎符合上述标准1。鉴于这不符合规范,我建议不要使用这个版本,因为它可能是依赖浏览器的(我不知道是否 10-03- 2010 将导致不同的日期,如果我的浏览器上设置了欧洲地区)。

I haven't found a spec that says Date.parse can handle mm/dd/yyyy dates, but my browser (Chrome 14) clearly can, and probably other browsers can too. This appears to follow standard 1 above, assuming the local timezone. Given that it's not in the spec, however, I would recommend not using this version, as it's likely to be browser-dependent (and I have no idea whether 10-03-2010 would result in a different date if I had a European locale set on my browser).

在大多数口译中,原始的 Date.parse 函数有一些问题 - 我经常遇到类似于你所描述的时区问题。因此,一般来说,我使用像 Datejs 这样的图书馆,或者我编写我自己的解析函数。 SIMILE AJAX库的 DateTime模块有一个很好的解析ISO-8601日期的示例函数(您所指的是 euroStyleDate ,加上可选的时间组件)。

There are a few issues with the native Date.parse function in most interpreters - I have often had timezone issues like the one you describe. So in general, I either use a library like Datejs or I write my own parsing functions. The DateTime module of the SIMILE AJAX library has a pretty good example function for parsing ISO-8601 dates (what you're referring to as euroStyleDate, plus an optional time component).

在设置日期时,我通常使用 new Date(),然后使用 setUTC *()函数将不同的日期元素设置为我想要的精度。这不完美,但至少你正在处理一个明确的时区。

When setting dates, I generally use new Date() and then use the setUTC*() functions to set the different date elements to my desired precision. It's not perfect, but at least you're dealing with a clear timezone.

这篇关于Javascript Date.parse错误时以虚线划分并以年开始的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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