JavaScript Time Parsing

I want to convert the string to a date object. The time may be in different formats, such as 2020-01-01 or 2020/01/01, or it may be accurate to the minute.

Time parsing in JavaScript sometimes gives strange results.

new Date("2021-01-01") // Fri Jan 01 2021 09:00:00 GMT+0900
new Date("2021-01-01 00:00") // Wed Jan 01 2020 00:00:00 GMT+0900

The first date I enter is thought to be UTC, and the second is thought to be local.

Is there any way to properly parse them?

89 thoughts on “JavaScript Time Parsing”

Leave a Comment