简体   繁体   中英

Moment.js returning wrong date

I'm trying to use moment to convert Date-Time for October 29th, 2013 with this format

2013-10-29T00:00:00.000Z

However when I do this

moment('2013-10-29T00:00:00.000Z').format("MMM Do, YYYY")

It returns October 28th, 2013 when it should return October 29th, 2013

If you have some ideas on how I can solve this issue, please let me know. Thank you

If you want the time in utc, use:

moment.utc('2013-10-29T00:00:00.000')

As @MattJohnson pointed out, using the moment constructor will translate it to local time. Instead (if you don't want to use the utc method), you can replace the Z with +0 . See options for string date/time http://momentjs.com/docs/#/parsing/string-format/

You can adjust the timezone settings by doing this:

moment('2013-10-29T00:00:00.000Z').zone(0).format("MMM Do, YYYY");

FIDDLE

Going to go ahead and add Matt's suggestion as well, since it is more semantic.

moment('2013-10-29T00:00:00.000Z').utc().format("MMM Do, YYYY");

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