formatting
Date.prototype.toISOString
Returns the date as an ISO 8601 extended-format string in UTC, always ending in 'Z'. This is the canonical machine-readable serialization and the format most APIs and databases accept.
date.toISOString() Parameters
| Parameter | Purpose |
|---|---|
| () | no arguments; returns 'YYYY-MM-DDTHH:mm:ss.sssZ' |
| UTC | always UTC regardless of host timezone |
| Invalid Date | throws RangeError |
Examples
console.log(new Date(0).toISOString()); // '1970-01-01T00:00:00.000Z' epoch
console.log(new Date('2026-07-19T12:34:56.789Z').toISOString()); // '2026-07-19T12:34:56.789Z' round-trip
try { new Date('bad').toISOString(); } catch (e) { console.log(e.name); } // 'RangeError' throws on invalid
Gotcha
Always UTC (Z suffix) - it does NOT show local time. Throws RangeError on Invalid Date, unlike most other formatters which return 'Invalid Date' as a string.
Related
Date.prototype.toJSON
Returns an ISO 8601 UTC string suitable for JSON serialization; called automatically by JSON.stringify when it encounters a Date. Equivalent to toISOString() for valid dates, but returns null for Invalid Dates instead of throwing.
Date.prototype.toUTCString
Returns the date as a string in the older RFC 7231 / IMF-fixdate format, e.g. 'Sun, 19 Jul 2026 00:00:00 GMT'. This is the exact format required by HTTP headers like Date, Expires, and cookies.
Date.parse
Parses a date string and returns the corresponding milliseconds since the Unix epoch, or NaN if unrecognized. Only the ISO 8601 subset is guaranteed portable; any other format is implementation-defined.
Date.prototype.toLocaleDateString
Returns the date portion formatted for a given locale and options, without the time. For repeated formatting, cache an Intl.DateTimeFormat instead - it is significantly faster.
Date.prototype.toLocaleTimeString
Returns the time portion formatted for a given locale and options, without the date. Same locale/options rules as toLocaleDateString.