Why is 2/8888/2016 a valid date in IE and Firefox?

If you take the following:

var s = "2/8888/2016"; var d = new Date(s); alert(d); 

In Chrome, you will get:

Invalid date

But in IE and Firefox you get:

Fri Jun 01 2040 00:00:00 GMT-0500 (Central Daylight Saving Time)

It seems that just 8888 days are added until February 01. Instead, I expect the date to be considered invalid. Is there a way to make FireFox and IE assume that this date string is invalid?

+7
javascript date
source share
3 answers

Short answer:

This is the misuse of the browsers you mention.

You must check the date in the correct format yourself. But this is pretty trivial, I propose this approach:

Separate the date in year y , month m , day d and create a Date object:

 var date = new Date( y, m - 1, d ); // note that month is 0 based 

Then compare the original values ​​with the boolean values ​​obtained using the Date methods:

 var isValid = date.getDate() == d && date.getMonth() == m-1 && date.getFullYear() == y; 

Before doing all this, you can check the correctness of the date string for any browser:

Invalid Date Detection JavaScript Date Instance



Long answer:

Firefox (and IE), accepting "2/8888/2016" as the correct string format, seems like an error / incorrect behavior.

In fact, according to the ECMAScript 2015 Language Specification, when Date() is called with one argument of a string, it should behave like Date.parse()

http://www.ecma-international.org/ecma-262/6.0/#sec-date-value

Last

tries to analyze the format of the string in accordance with the rules (including long years), is called in the format of the date time string (20.3.1.16)

.. which is listed here

http://www.ecma-international.org/ecma-262/6.0/#sec-date-time-string-format

where can you read

The format is as follows: YYYY-MM-DDTHH: mm: ss.sssZ

[...]

MM - month of the year from 01 (January) to 12 (December).

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


Firefox seems to interpret the string value as if calling Date() with several arguments.

FROM

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date

Note. . If Date is called as a constructor with several arguments, if values ​​are greater than their logical range (for example, 13 is provided as the value of the month or 70 for the minute value), the adjacent value will be adjusted. For example. the new date (2013, 13, 1) is equivalent to the new date (2014, 1, 1), both create a date for 2014-02-01 (note that the month is based on 0). Similarly for other values: the new date (2013, 2, 1, 0, 70) is equivalent to the new date (2013, 2, 1, 1, 10), which creates a date for 2013-03-01T01: 10: 00.

This may explain how "2/8888/2016" turns into 2040-05-31T22:00:00.000Z

+3
source share

It is not possible to force IE and FF to consider this invalid, except for:

  • you can change your javascript implementations
  • instead you use a library.

We can also expect Javascript, as a language, to evolve, and we can cross our fingers so that browsers decide to follow a more strict specification. The problem, of course, is that every β€œfix” should also be backward compatible with previous versions of the language (not always, Perl, for example).

So, it is best to use some library just like momentjs , as Derek suggested in the comments on the post.

+1
source share

You stumbled upon another reason why you must manually parse date strings.

When Date is given a single string argument, it is treated as a date string and parsed according to the rules in Date.parse . The rules there first try to parse it as an ISO 8601 string. If this does not work, it can return to any parsing algorithm that it wants.

In the case of "2/8888/2016", browsers will first try to analyze it as an ISO format and fail. It seems from the experiments that IE and Firefox determine that the string is in the format month / day / year and effectively calls the Date constructor with:

 new Date(2016,1,8888); 

However, other browsers may try to check the values ​​and decide that 8888 is not a valid date or month, so return an invalid date. Both answers correspond to ECMA-262.

The best advice is to always manually analyze the date strings (the library can help, but it is usually not needed, since the parse parse function with verification is 3 lines of code), then you can be sure of the consistent results in any browser or host environment.

0
source share

All Articles