Understanding Date.prototype.toISOString() ISO 8601 format?
Asked Answered
A

1

6

Quote from MDN:

The toISOString() method returns a string in simplified extended ISO format (ISO 8601), which is always 24 or 27 characters long (YYYY-MM-DDTHH:mm:ss.sssZ or ±YYYYYY-MM-DDTHH:mm:ss.sssZ, respectively).

  1. When will the second format ±YYYYYY-MM-DDTHH:mm:ss.sssZ be returned?
  2. What means the ±YY in the beginning of ±YYYYYY-MM-DDTHH:mm:ss.sssZ?
Astrogate answered 25/2, 2021 at 12:46 Comment(1)
MDN is a community resource, it's not normative. ECMA-262 is the authority, see §21.4.1.15. The extra digits are for the ISO 8601 expanded year format that allows Dates to cover ±273,790 years from 1970, which is the possible range of ECMAScript dates. You should never see years with more than 4 digits in practice.Djerba
S
7

As the spec says, it will be returned when the year is before 1 AD:

const d = new Date()
// Thu Feb 25 2021 14:49:43 GMT+0200 (Eastern European Standard Time)
d.setFullYear(-7731)
// -306129149405605
console.log(d.toISOString())
// "-007731-02-25T13:09:54.395Z"

or suitably far into the future.

const d = new Date();
d.setFullYear(11931)
// 314343550183395
console.log(d.toISOString())
// "+011931-02-25T12:49:43.395Z"
Sporule answered 25/2, 2021 at 12:51 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.