CKTIS / JavaScript
Date.prototype.toISOString: canonical UTC text
toISOString returns canonical UTC date-time text with milliseconds and a trailing Z.
Version: ECMAScript 5 (2009) onward; example target: Node.js 24.22.2
Syntax
date.toISOString()date.toISOString() returns the simplified ISO 8601 format in UTC: YYYY-MM-DDTHH:mm:ss.sssZ. Ordinary years produce 24 characters; years outside 0000–9999 use a sign and six digits, producing 27. The method does not use the local time zone and does not mutate the object.
For an invalid Date, or a year that cannot be represented in the required format, it throws RangeError; it does not return "Invalid Date". A receiver without Date internal state throws TypeError. The method takes no arguments, and extra arguments are ignored.
The format is suitable for data interchange and can be parsed again by Date.parse. It preserves neither a zone name nor the original offset, only the same instant expressed in UTC. The method has been standard since ECMAScript 5 (2009).
Examples
const date = new Date(Date.UTC(2024, 1, 29, 12, 34, 56, 789));
console.log(date.toISOString());
console.log(new Date(-1).toISOString());
try { new Date(NaN).toISOString(); } catch (error) { console.log(error instanceof RangeError); }Result
2024-02-29T12:34:56.789Z 1969-12-31T23:59:59.999Z true