2
console.log(new Date("2000-02-31"));

The above will work. It will it convert to 2000-03-02T00:00:00.000Z.

How do I override this behavior and get it to throw an error on garbage input?

  • 1
    Febrarury only has 29 days at most – Bear Bile Farming is Torture Nov 19 '21 at 03:07
  • Any implementation that parses "2000-02-31" as a valid date is not compliant with [ECMA-262](https://262.ecma-international.org/#sec-date.parse), it must return an invalid Date: "*Strings that are unrecognizable or contain out-of-bounds format element values shall cause Date.parse to return NaN*". – RobG Nov 19 '21 at 03:07
  • There are a lot of date libraries that are useful for this sort of thing. Check out Moment.js. They're quite chonky though. – Brad Nov 19 '21 at 03:08
  • Oh, to answer "*How do I override this behavior and get it to throw an error on garbage input?*" you can't, you can only parse it separately and throw an error yourself. – RobG Nov 19 '21 at 03:13

0 Answers0