为什么JavaScript将1/1/0000分配为1/1/2000? [英] Why does JavaScript interprete 1/1/0000 as 1/1/2000?

查看:93
本文介绍了为什么JavaScript将1/1/0000分配为1/1/2000?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一些奇怪的行为,虽然我没有真正的问题,但我想知道背后的原因。



我写了以下陈述:

  console.log(new Date(0000-1-1)); 
console.log(new Date(0000-01-01));
console.log(new Date(0000-01-01T00:00:00Z));
console.log(new Date(0,0,1));

虽然所有日期出现相似,如果您使用正常的日/月/年行为



结果如下:

  Sat Jan 01 2000 00:00:00 GMT + 0100(CET)
星期六01 01 01 01:00:00 GMT + 0100(CET)
星期六01 01 01 01:00 :00 GMT + 0100(CET)
Mon Jan 01 1900 00:00:00 GMT + 0100(CET)

对我来说很有趣我可以同意最后一个,因为 JavaScript规范清楚地表示如下:




表示年份的整数值。从0到99的值映射到1900年到1999年。见下面的例子。


我也可以理解第二和第三个结果,因为这取决于 RFC 2822 。他们一年接受 4 * DIGITS ,所以 0000 应该是RFC规范的第0年(我没有



可以使用其中一个 Date 构造函数有点奇怪对于1900年以上的日期,而另一个构造函数允许日期范围更广(RFC)。



但是,我不明白第一个结果。 2000年来自哪里?我明白这个日期并不是一个合适的日期,但不应该返回无效的日期而不是这个日期?

解决方案

这是特定于浏览器的。


浏览器不一致会导致额外的程序员痛苦。一些浏览器会将所有两位数年份解释为19xx,所以新日期('1/1/49')给出了1949年1月1日和新的日期('1/1/50')于1950年1月1日提交,其他人将1950年作为两位数年度截止日期,所以新日期('1/1 / code>于2049年1月1日和新日期('1/1/50')于1950年1月1日发布。 p>

JavaScript中的两位数字 - Chris Bristol


您必须记住,您引用的RFC文档是在2001年4月发布的.2000年代刚刚结束。我想为了使二千年代的日期现代化,一些浏览器现在将0到49映射到2000年至2049年。然而,50仍然映射为1950年。



上面引用的文章还会给出一些测试结果:


以下是结果的简要摘要:




  • IE9:找不到两位数的年限。年份00 = 1900。

  • Chrome 24.0:两位数年龄在49和50之间。年份00 = 2000。

  • 歌剧:没有两位数的年份截断发现。年份00 = 1900。

  • Firefox:找不到两位数的年限。年份00 = 1900。

  • Safari:两位数年份在49和50之间变化。00 00 = 2000。


< blockquote>

这篇文章已经过时了,所以我想像上面的Opera和Firefox的数据可能已经改变了。


I encountered some strange behaviour, and while I don't really have an issue with it, I would like to know the reasoning behind it.

I wrote the following statements:

console.log(new Date("0000-1-1"));
console.log(new Date("0000-01-01"));
console.log(new Date("0000-01-01T00:00:00Z"));
console.log(new Date(0, 0, 1));

While all dates "appear" to be similar, and if you use normal days/months/years behave the same, it does not in this case.

The results are the following:

Sat Jan 01 2000 00:00:00 GMT+0100 (CET)
Sat Jan 01 0 01:00:00 GMT+0100 (CET)
Sat Jan 01 0 01:00:00 GMT+0100 (CET)
Mon Jan 01 1900 00:00:00 GMT+0100 (CET)

Which is quite interesting to me. I can agree with the last one, because the JavaScript spec clearly says the following:

year

Integer value representing the year. Values from 0 to 99 map to the years 1900 to 1999. See the example below.

I can also understand the second and third result, since this is depending on the RFC 2822. They do accept 4*DIGITS as a year, so 0000 should be the year 0 by the RFC spec (I didn't read it completely though).

It's only a bit weird that one of the Date constructors can be used for dates above 1900 while the other constructor allows dates from a wider range (RFC).

However, I do not understand the first result. Where does the year 2000 come from? I do understand that the date is not really an appropriate one, but shouldn't it return Invalid Date in stead of this date?

解决方案

This is browser specific.

This is yet another area where JavaScript and browser inconsistency cause extra programmer pain. Some browsers will interpret all two digit years as 19xx, so new Date('1/1/49') gives January 1st, 1949, and new Date('1/1/50') gives January 1st, 1950. Others use 1950 as the "two digit year cutoff date", so new Date('1/1/49') gives January 1st, 2049, and new Date('1/1/50') gives January 1st, 1950.

Two Digit Years in JavaScript - Chris Bristol

You have to bear in mind that the RFC document you've referenced was published in April 2001. The 1900s had only just ended. I guess in an attempt to modernise dates away from the 1900s, some browsers now map 0 to 49 as 2000 to 2049. 50, however, still gets mapped as 1950.

The article quoted above also goes on to give some test results:

Here is a brief summary of the results:

  • IE9: No two digit year cutoff found. Year 00 = 1900.
  • Chrome 24.0: Two digit years change between 49 and 50. Year 00 = 2000.
  • Opera: No two digit year cutoff found. Year 00 = 1900.
  • Firefox: No two digit year cutoff found. Year 00 = 1900.
  • Safari: Two digit years change between 49 and 50. Year 00 = 2000.

The article is quite dated now though, so I imagine the data for Opera and Firefox above may have changed.

这篇关于为什么JavaScript将1/1/0000分配为1/1/2000?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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