Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

javascript Date.parse assumes 31 days in February and all months?

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
like image 362
barlop Avatar asked Aug 29 '15 10:08

barlop


People also ask

What is parse date in JavaScript?

Date.parse() The Date.parse() method parses a string representation of a date, and returns the number of milliseconds since January 1, 1970, 00:00:00 UTC or NaN if the string is unrecognized or, in some cases, contains illegal date values (e.g. 2015-02-31).

Why are JavaScript months zero based?

because they might have an array of string (indexed from 0) of month names and these month numbers if they start from 0, it'll be lot easier to map to the month strings.. Save this answer.

What date format does JavaScript use?

The three Javascript date format types are ISO (International Organization for Standardization) dates, short dates, and long dates. By default, Javascript uses ISO dates internally.

What can I use instead of date parse?

If the DATEPARSE function is not available for the data that you're working with, or the field you are trying to convert is a number data type, you can use the DATE function instead.


1 Answers

According to the specification for Date.parse() (emphasis mine):

The function first attempts to parse the format of the String according to the rules called out in Date Time String Format. […] Unrecognisable Strings or dates containing illegal element values in the format String shall cause Date.parse to return NaN.

And according to the specification for Date Time String Format (emphasis mine):

ECMAScript defines a string interchange format for date-times based upon a simplification of the ISO 8601 Extended Format. The format is as follows: YYYY-MM-DDTHH:mm:ss.sssZ

Where the fields are as follows: […] DDis the day of the month from 01 to 31.

Therefore, any date with a day of month greater than 31 is illegal and Date.parse() returns NaN.

Please notice that the standard defines a date format, not a date: the static method isn't required to make additional verifications, and everything else is implementation-specific. For instance, Date.parse('2013-02-30') and Date.parse('2013-04-31') both return NaN on Firefox.

like image 78
Blackhole Avatar answered Oct 01 '22 02:10

Blackhole