Pan Wangperawong
Pan Wangperawong

Reputation: 1250

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

Upvotes: 23

Views: 38855

Answers (2)

Jeff Storey
Jeff Storey

Reputation: 57192

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/

Upvotes: 25

kalley
kalley

Reputation: 18462

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");

Upvotes: 8

Related Questions