I'm trying to get the standardized ISO 8601 date format string from Moment.js, but I'm not getting the result I expect.
Moment's toISOString()
method is giving me an output with a 17-hour offset, instead of midnight:
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 that format?
I'm trying to get the standardized ISO 8601 date format string from Moment.js, but I'm not getting the result I expect.
Moment's toISOString()
method is giving me an output with a 17-hour offset, instead of midnight:
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 that format?
-
1
Because by default moment parses and displays in local time, while
.toISOString()
always returns a timestamp in UTC. You probably have -7 hours offset from UTC. Useformat()
if you want to display date in local time. If your input string represents a UTC time, then usemoment.utc(String, String);
– VincenzoC Commented Nov 25, 2016 at 11:32
1 Answer
Reset to default 16As 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);