MomentJS toISOString result is different?

2019-09-20 01:18发布

问题:

Is something wrong with code.

var mom = moment("23-11-2016 00:00", "DD-MM-YYYY HH:mm");
alert(mom.toISOString());
//result 2016-11-22T17:00:00.000Z

Why the result is not 2016-11-23T00:00:00.000Z? How I can get 2016-11-23T00:00:00.000Z result?

Thanks in advance.

回答1:

As the doc says:

By default, moment parses and displays in local time.

while .toISOString() always returns a timestamp in UTC:

Note that .toISOString() always returns a timestamp in UTC, even if the moment in question is in local mode. This is done to provide consistency with the specification for native JavaScript Date .toISOString(), as outlined in the ES2015 specification.

You probably have -7 hours offset from UTC.

Use format() if you want to display date in local time.

If your input string represents a UTC time, then use moment.utc(String, String);



标签: momentjs