Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MomentJS date string adds one day

Tags:

date

momentjs

I don't understand why this date is saved as +1 day:

startdate = "2017-11-29T23:59:59.999Z";
var new_date = moment(startdate).format('DD/MM/YYYY'); // --> gives 30/11/2017

But if I do:

startdate = "2017-11-29";
var new_date = moment(startdate).format('DD/MM/YYYY'); // --> gives the correct date 29/11/2017

Any ideas?

Here is a jsfiddle showing this: http://jsfiddle.net/jbgUt/416/

Thanks!

like image 339
Anna Avatar asked Nov 28 '17 10:11

Anna


1 Answers

If a time part is included, an offset from UTC can also be included as +-HH:mm, +-HHmm, +-HH or Z.

Add utc() to avoid it.

moment(startdate).utc().format('DD-MM-YYYY')

or

moment.utc(startdate).format('DD-MM-YYYY')

If you want to parse or display a moment in UTC, you can use moment.utc() instead of moment()

like image 118
Mikhail Katrin Avatar answered Nov 03 '22 09:11

Mikhail Katrin