3

I was trying to check what values would return if partial values are sent.

var d = new Date("11/28");
console.log(d.toLocaleDateString())

I was expecting it to be 28th Nov., 2016(current year), but it returns 28th Nov., 2001.

So the question is Why is 2001 considered as default year?

Rajesh
  • 24,354
  • 5
  • 48
  • 79

2 Answers2

5

2001 is not consider as default year. This is a Chrome issue, if you run the same code with Firefox you get Invalid Date.

Bellu
  • 2,575
  • 2
  • 22
  • 29
  • Though this is the correct answer, it will not add much to SO. Can you please remove answer, so I can delete the post? – Rajesh Nov 09 '16 at 10:12
1

The language specification only includes rules for parsing ISO 8601 formatted strings. Parsing of any other format is implementation dependent (noting that the Date constructor and Date.parse are equivalent for parsing).

So given "11/28" is not a valid ISO 8601 string, the implementation is free to apply any heuristics it likes. Any result, including an invalid date, should be expected and consistency between implementations should not.

RobG
  • 142,382
  • 31
  • 172
  • 209