Skip to content
Tutorials Center
My learning

Topics

JavaScript Reference ยท Date and Intl

Date.prototype.toISOString()

Reference for JavaScript Date.prototype.toISOString(): format a date as an ISO 8601 UTC string, serialize dates in JSON, and avoid the date-only trap.

toISOString() converts a Date into a string in the ISO 8601 format, always in UTC: YYYY-MM-DDTHH:mm:ss.sssZ. The trailing Z means "zero offset", that is, UTC. The format sorts correctly as plain text, is understood by every platform, and can be passed straight back to new Date().

It is the standard way to send dates between systems, store them in databases and write them in logs.

JavaScript
new Date(0).toISOString(); // "1970-01-01T00:00:00.000Z"

Try it

JS ISO strings and round trips

Press Run (or Ctrl + Enter) to see the console output here.

No output.

JS Invalid dates and the date-only trap

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Syntax

JavaScript
date.toISOString()

Parameters: none.

Return value: a string of 24 characters, such as "2026-03-15T09:30:00.000Z". Years outside 0000 to 9999 use an expanded format with a sign and six digits (+275760-09-13T00:00:00.000Z).

Exceptions: a RangeError if the date is invalid.

Usage notes

  • Always UTC. The string never contains your local offset. The same instant gives the same string on every computer.
  • Throws on invalid dates, unlike toString(), which returns "Invalid Date". Check with Number.isNaN(date.getTime()) first when the input is untrusted.
  • JSON.stringify() uses it. Date.prototype.toJSON() returns the ISO string (or null for an invalid date), so dates inside objects serialize automatically. JSON.parse() does not turn them back into dates; wrap the value in new Date() yourself.
  • slice(0, 10) is the UTC date, not necessarily the local calendar date. For a user in the evening west of Greenwich, it can already show tomorrow. Build the local date from getFullYear(), getMonth() and getDate() instead.
  • Millisecond precision. There are always three fractional digits, even for zero.
  • Sorting. ISO strings of the same length sort chronologically as text, handy for logs and keys.
  • Not for display. Use toLocaleDateString() or Intl.DateTimeFormat for text that people read.

Browser support

toISOString() works in every browser and runtime.