Moment.js round dates up

2019-08-10 11:22发布

问题:

In my javascript the library Moment.js rounds my dates up.

 Date: 2015-02-09T23:00:00.000Z

 moment(Date).format('DD/MM'); ==> Becomes 10/02

I want 09/02 as result. Is there a possible way that the library not rounds the date?

回答1:

You can try like below.

moment(Date,['YYYY-MM-DD']).format('DD/MM');


回答2:

The problem is likely one of timezones: by default, momentjs parses your string and converts it to your local timezone. If I see this correctly, the 'Z' in your date signifies zulu - or UTC time. If your timezone is +02:00 for example, that would make it the 10th, 01:00.

Use Moment#utc

moment(Date).utc().format('DD/MM');

to output format the date as UTC again.



回答3:

Moment.js will output dates in the local time zone, so it might very well be that it's caused by a difference in timezones.

If you want to show the date/time as encoded into the original string, use parseZone like this:

var dateStr = "2015-02-09T23:00:00.000Z";
moment.parseZone(dateStr).format('DD/MM');