Meaning of [Z] in format + string in Moment.js
Z
does not cause the time to be treated as UTC when used in the format. It matches a timezone specifier:
Format: Z ZZ
Example: +12:00
Description: Offset from UTC as +-HH:mm, +-HHmm, or Z
And under the documentation for format
:
To escape characters in format strings, you can wrap the characters in square brackets.
By specifying Z
in brackets, you are matching a literal Z
, and thus the timezone is left at moment's default, which is the local timezone.
Unless you specify a time zone offset, parsing a string will create a date in the current time zone.
If your time is really in UTC, this is probably not desired behavior. If you want to parse it as UTC but display it in local time, use Z
and then call local()
on the resulting moment
object, so most likely what you want is:
// Parse with timezone specifier (which is UTC here) but convert to local time
newM = moment("2015-08-11T13:00:00.000000Z", "YYYY-MM-DDTHH:mm:ss.SSSSZ", true).local();
For everyone who cares about consistency due to the missing Z
at the end - try to add [Z]
to your format.
Example: .format('YYYY-MM-DDTHH:mm:ss[Z]')
so the result is: 2019-11-26T10:39:54Z
More detailed explanation you can find in this github issue.