3

new Date((new Date('1900-01-01')).getTime())
--> Sun Dec 31 1899 23:23:15 GMT-0036 (Western European Standard Time)

but I would expect the above to be the same as this:
new Date((new Date('1900-01-01T00:00')).getTime())
--> Mon Jan 01 1900 00:00:00 GMT-0036 (Western European Standard Time)

which is does for all dates after ~1912-01-01:
new Date((new Date('1912-01-01')).getTime())
--> Mon Jan 01 1912 00:00:00 GMT+0000 (Western European Standard Time)

even more weird:
new Date((new Date('1911-11-3')).getTime())
--> Fri Nov 03 1911 00:00:00 GMT-0036 (Western European Standard Time) BUT
new Date((new Date('1911-11-03')).getTime())
--> Thu Nov 02 1911 23:23:15 GMT-0036 (Western European Standard Time)

I know there are good reasons that Moment.js exists, but why on earth is there a difference in time with and without that leading zero in the date??

(new Date('1911-11-03')).getTime()
-1835481600000
(new Date('1911-11-3')).getTime()
-1835479395000
gotjosh
  • 831
  • 3
  • 9
  • 18
  • 2
    `1911-11-3` is not a valid date time string according to the specification. Parsing it is thus implementation-dependent. – str Aug 10 '20 at 11:09
  • 1
    `new Date((new Date('1900-01-01')).getTime())` produces an identical result to `new Date('1900-01-01')`. Per ECMA-262, "1900-01-01" is parsed as UTC (blame the TC39 for that poor decision). "1900-01-01T00:00" (no offset) is parsed as local (except Safari, which is buggy). Note that historical timezone offsets are applied, prior to about 1900 most were not even hour or half hour offsets. "1911-11-3" is not a supported format so parsing is implementation dependent, many will produce an invalid date. – RobG Aug 10 '20 at 11:25

0 Answers0