Search code examples
javascriptdatedatetimedatetime-formatiso

Difference between ISO formatting "+00:00" and "Z"?


What's the difference between this date format:

"2021-01-01T00:00:00.000Z"

and this one:

"2021-01-01T00:00:00+00:00"

Are they both valid ISO formats? When I call new Date().toISOString() in JavaScript, I get the first one.

For some reason, I'm having trouble finding a definitive answer on this.


Solution

  • ECMA-262 defines a "string interchange format" that is generated by toISOString and must also be parsed by the built–in parser. It states:

    Z is the UTC offset representation specified as "Z" (for UTC with no offset) or an offset of either "+" or "-" followed by a time expression HH:mm (indicating local time ahead of or behind UTC, respectively)

    So while toISOString produces "Z", the parser will also accept "+00:00" and "-00:00". Correct parsing of any other format is implementation dependent (and hence unreliable).