JavaScript Date.parse在二月和所有月份中假定31天? [英] javascript Date.parse assumes 31 days in February and all months?

查看:179
本文介绍了JavaScript Date.parse在二月和所有月份中假定31天?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我看来,Date.parse假定所有月份都有31天。包括30天的月份,包括二月(只有28/29天)。



我检查了这个问题 2月31日在Date对象



但答案有人建议在他的问题中没有任何错误。有人向询问者说了零索引,他几乎说哦,,并且认为这是他的错误,而不是Date的错误。



另一个问题日期正在考虑31天的那个月,这个人正在做一些减法是一些代码行,他似乎没有把错误降到最后的日期。



但是这个例子似乎有点不同,更清晰。它涉及Date.parse,可以用一行/两行代码进行演示。



Date.parse知道一个月内没有32天,这很好

  Date.parse(2000-01-32); 
NaN

但是在二月份,它认为可能会有30天或31天

  Date.parse(2013-02-30); 
1362182400000

Date.parse(2013-02-31);
1362268800000

实际上,它看起来像是所有月份都有31天。这是一个非常奇怪的方法来解析一个日期。



这里没有零索引的问题。由于Date.parse(...)不使用零索引(即使是这样,它也不会导致它在二月份有31天的错误 - 这是多个关闭!

  Date.parse(01-00-2000); 
NaN

Date.parse(00-01-2000);
NaN


解决方案

浏览器之间的实现不同,IE,Edge和Chrome将解析不代表实际日期的字符串,但Firefox将返回 NaN 安全的做法是将 Date.parse 的结果作为未定义的日期字符串的日期不在本月的范围内。



允许解析不存在日期的浏览器将返回不同的日期。解析2015-04-31将返回日期 2015-05-01 这与使用新日期(2015年3月31日)时相同的行为,其中数字超出范围是允许的,我绕着不同的月份或年份。这意味着结果仍然可用,如果您不介意某些浏览器中的某些无效日期变成其他日期。



标准对于什么值是有效的不太清楚:


格式
字符串中的非法值(超出范围和语法错误)意味着格式字符串不是有效的这个
格式。


日期组件定义为范围从1到31:


DD是01到31月的日子。


但是,格式基于 ISO 8601 ,而且不是将字符串解析为格式日期,这是用于将日期表示为字符串的格式。很明显,你不能代表一个甚至没有字符串的日期。


It seems to me that Date.parse assumes all months have 31 days. Including months with 30 days and including February(which should only ever have 28/29 days).

I checked this question 31 days in February in Date object

But the answer there suggested there was nothing wrong with Date in his issue..Somebody said something to the questioner about zero indexing and he pretty much said "oh ok", and determined that it was his mistake and not a mistake of Date.

Another question Date is considering 31 days of month the guy was doing some subtraction was a number of lines of code and he seemed to not put the error down to Date in the end.

But this example that I have seems to be a bit different and more clear cut. It involves Date.parse and can be demonstrated with one/two lines of code.

Date.parse is aware that there are not 32 days in a month, that's good

Date.parse("2000-01-32");
NaN

But In February it thinks there can be 30 or 31 days

Date.parse("2013-02-30");
1362182400000

Date.parse("2013-02-31");
1362268800000

In fact it looks like it thinks all months have 31 days. That is really strange for a method that is meant to parse a date.

And there's no issue of zero indexing here. As Date.parse("...") doesn't use zero indexing (And even if it did, it wouldn't cause it tot make the error of thinking there are 31 days in February - that is more than one off!

Date.parse("01-00-2000");
NaN

Date.parse("00-01-2000");
NaN

解决方案

The implementation differs between browsers. IE, Edge and Chrome will parse strings that doesn't represent actual dates, but Firefox will return NaN for those strings. The safe thing to do is to consider the result from Date.parse as undefined for date strings where the day falls outside the range of the month.

Browsers that allow parsing of non-existent dates will return a different date. Parsing "2015-04-31" will return the date 2015-05-01. This is the same behaviour as when using new Date(2015, 3, 31), where numbers out of range is allowed and will wrap around into a different month or year. That means that the result is still usable, if you don't mind that some invalid dates turn into other dates in some browsers.

The standard isn't very clear about what values are valid:

Illegal values (out-of-bounds as well as syntax errors) in a format string means that the format string is not a valid instance of this format.

The day component is defined as having a range from 1 to 31:

DD is the day of the month from 01 to 31.

However, the format is based on ISO 8601, and that is not a format for parsing strings into dates, that is a format for representing dates as string. Clearly you can't represent a date that doesn't even exist as a string.

这篇关于JavaScript Date.parse在二月和所有月份中假定31天?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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