简体   繁体   中英

I am getting wrong date after converting by moment.js

My code is

console.log(moment(new Date('2016-05-24T18:50:05.000')).format('LL'));

It should be 24 may 2016 but it gives me 25 may 2016 .

Can any one help me.

You need to specify the date as UTC.

You can do so in either of the following ways:

console.log(moment(new Date('2016-05-24T18:50:05.000Z')).format('LL'));

Where, the Z specifies that the time is UTC. Or:

console.log(moment.utc(new Date('2016-05-24T18:50:05.000')).format('LL'));


You can pass an ISO 8601 date string to moment directly.

There is no need to wrap the string in a javascript Date object. Then your code becomes:

console.log(moment('2016-05-24T18:50:05.000Z').format('LL'));

Or:

console.log(moment.utc('2016-05-24T18:50:05.000').format('LL'));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM