Warm tip: This article is reproduced from serverfault.com, please click

Moment.js .toDate() returns one year later than expected result

发布于 2020-11-28 01:14:39

I have a date. 2019-12-13 13:40:00 which stands for December 13th 2019 at 1:40 pm. When I parse this with moment.js...

moment(props.data[index].date, 'gggg-MM-DD HH:mm:ss')

the _d shows that the date is Sun Dec 13 2020 14:30:00 GMT-0800 (Pacific Standard Time). I have seen many explanations on the moment GitHub where people have this issue but its related to time zones, however an entire year difference is not a time zone problem. What can I do to fix this?

Questioner
SwimMaster
Viewed
11
Rajat Vijay 2020-11-28 09:25:04

gggg in the second argument is for parsing in ISO week date format. Try doing moment(props.data[index].date, 'YYYY-MM-DD HH:mm:ss')