Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Moment.js returning wrong date

Tags:

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

like image 288
Pan Wangperawong Avatar asked Oct 29 '13 02:10

Pan Wangperawong


2 Answers

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/

like image 146
Jeff Storey Avatar answered Sep 21 '22 15:09

Jeff Storey


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");
like image 31
kalley Avatar answered Sep 20 '22 15:09

kalley