user2224250
user2224250

Reputation: 251

Moment returns wrong datetime

I am using React as a front-end framework, python as a back-end language.

Python returns UTC datetime to React. Somehow, react+moment shows wrong local datetime (Berlin timezone). May I know, what to fix this issue ?

Case 1 is correct, but case 2 is wrong

Case 1:

Python returns to React: "2019-10-02T22:00:00Z"

React+Moment returns: Thu Oct 03 2019 00:00:00 GMT+0200 (Central European Summer Time) {}

moment('2019-10-02T22:00:00Z').toDate()

Thu Oct 03 2019 00:00:00 GMT+0200 (Central European Summer Time)

Case 2:

Python returns to React: "2019-10-30T22:00:00Z"

React+Moment returns: Wed Oct 30 2019 23:00:00 GMT+0100 (Central European Standard Time) {}

 moment('2019-10-30T22:00:00Z').toDate()

 Wed Oct 30 2019 23:00:00 GMT+0100 (Central European Standard Time)

Upvotes: 1

Views: 541

Answers (1)

Mezbaul
Mezbaul

Reputation: 1272

Python is using utc while moment.js is using an extra offset of +1 or whatever (your local time). If you want moment.js to use utc too, do this:

const m = moment.utc('2019-10-30T22:00:00Z')
console.log(m.toDate())

Upvotes: 2

Related Questions