Patrick Simpson

What The Actual Dates, JavaScript?

Dates in JavaScript are pretty frustrating...

new Date(Date.parse('2022-01-04')) what would you expect?

Answer: Mon Jan 03 2022 19:00:00 GMT-0500 (Eastern Standard Time)

This makes a sense, a little... I realize this is the localized version of the UTC time 2022-01-04 (at exactly 12am), but why is this the default? It can make dates off by one everywhere, if you are parsing in input or reading simple strings from a database... ack!

new Date(2022, 1, 4) what would you expect?

Answer: Fri Feb 04 2022 00:00:00 GMT-0500 (Eastern Standard Time)

Yep, that's February not January...

If you are scratching your head, you are not alone. For some reason, the month is zero based, while everything else is not. Not sure about this one honestly, but it has tripped me up a bit.

How about, setYear, setMonth, setDay.... wait no it's setDate (for day)?! And setYear is deprecated too!

Anyway, back to my madness...