When converting a date string in the format "YYYY-mm-dd" to a Date object in JavaScript, I'm seeing an inconsistency in the output that I don't understand. I get different times when a single-digit day of month is preceded with a zero. I'm hoping someone can explain this to me.
Here is a console snippet to show what I mean, the first one being the inconsistent case:
d = new Date('2020-01-09');
Wed Jan 08 2020 19:00:00 GMT-0500 (Eastern Standard Time)
d = new Date('2020-01-9');
Thu Jan 09 2020 00:00:00 GMT-0500 (Eastern Standard Time)
d = new Date('2020-1-9');
Thu Jan 09 2020 00:00:00 GMT-0500 (Eastern Standard Time)
d = new Date('2020-01-019');
Sun Jan 19 2020 00:00:00 GMT-0500 (Eastern Standard Time)
d = new Date('2020-01-09 EST');
Thu Jan 09 2020 00:00:00 GMT-0500 (Eastern Standard Time)
So when the day is two digits and the first one a zero, it gives me a time that is five hours earlier, notably the same offset as the time zone. Preceding zeroes on two digit days make no difference, nor does preceding the date.
Note also the last line, where I use the same format but tack on the time zone as well. That seems to correct the problem.
Could somebody explain why this is happening?