JavaScript .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).

  • Learn how to build back-end web APIs using Express.js, Node.js, SQL, and a Node.js-SQLite database library.
    • Includes 8 Courses
    • With Certificate
    • Beginner Friendly.
      30 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours

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

  • Learn how to build back-end web APIs using Express.js, Node.js, SQL, and a Node.js-SQLite database library.
    • Includes 8 Courses
    • With Certificate
    • Beginner Friendly.
      30 hours
  • Learn how to use JavaScript — a powerful and flexible programming language for adding website interactivity.
    • Beginner Friendly.
      15 hours