.toISOString()

BrandonDusch's avatar
Published Jun 2, 2022
Contribute to Docs

The .toISOString() method returns a string representation of a date in an ISO 8601-compliant format according to universal time (UTC).

Syntax

date.toISOString();

The string returned from the .toISOString() method will always be 24 or 27 characters long such as YYYY-MM-DDThh:mm:ss.sssZ or ±YYYYYY-MM-DDThh:mm:ss.sssZ, respectively.

Example

In the example below, today’s date is declared and stored in a variable. It is then invoked against the .toISOString() method:

const today = new Date();
console.log(today.toISOString());

The snippet above will output something similar to the following:

2022-04-22T18:12:21.369Z

Codebyte Example

The following example further demonstrates how the .toISOString() method is used:

Code
Output
Loading...

All contributors

Contribute to Docs

Learn JavaScript on Codecademy