为什么javascript会以不同的方式解释这些相同的日期 [英] Why does javascript interpret these identical dates differently

查看:120
本文介绍了为什么javascript会以不同的方式解释这些相同的日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里发生了什么:

> new Date('Apr 15 2013');
Mon Apr 15 2013 00:00:00 GMT+0100 (GMT Daylight Time)
> new Date('04/15/2013');
Mon Apr 15 2013 00:00:00 GMT+0100 (GMT Daylight Time)
> new Date('2013-04-15');
Mon Apr 15 2013 01:00:00 GMT+0100 (GMT Daylight Time)

显然,一个被解释为UTC时间,而另外两个被解释为本地时间。什么原因导致解析的差异?

Obviously, one is being interpreted as UTC time, while the other two are being interpreted as local time. What causes the difference in parsing?

推荐答案

规范


字符串可能被解释为本地时间,UTC时间或某个其他时区中的时间,具体取决于String 的内容。该函数首先尝试根据日期时间字符串格式中调用的规则来解析String的格式( 15.9.1.15 )。如果String不符合该格式,该函数可能会退回到任何实现特定的启发式或实现特定日期格式。

The String may be interpreted as a local time, a UTC time, or a time in some other time zone, depending on the contents of the String. 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.

在您提供的所有格式中,只有'2013-04-15'正式支持,所以其他格式似乎落后于依赖于实现的行为。

Of all the formats you provided, only '2013-04-15' is officially supported, so the others seem to fall back to implementation-dependent behavior.

这篇关于为什么javascript会以不同的方式解释这些相同的日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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