JavaScript Date.toISOString() Method

Shubham Vora Jan 30, 2023
  1. Syntax of JavaScript date.toISOString():
  2. Example Code: Use the date.toISOString() Method to Get the Current Date and Time in ISO Format
  3. Example Code: Use the date.toISOString() Method to Convert the Specified Date and Time Into the ISO Format
JavaScript Date.toISOString() Method

The date.toISOString() method changes the date object into a string using the format that is internationally accepted to represent dates and times. The format that the .toISOString() method uses is the YYYY-MM-DDTHH:mm:ss.sssZ string format.

Syntax of JavaScript date.toISOString():

let date = new Date();
date.toISOString();

Parameter

This method does not take any parameters.

Return

This method returns a string with the date and time in an internationally accepted format.

Example Code: Use the date.toISOString() Method to Get the Current Date and Time in ISO Format

In JavaScript, we can use the date.toISOString() method to get the date and time in an internationally accepted format. As mentioned above, we do not need to pass a parameter to the .toISOString() method.

We created a date object in this example using the new Date() method. We used the date.toISOString() method to get the current date and time in ISO format.

const date = new Date();
let iso = date.toISOString();
console.log(iso);

Output:

2022-08-17T14:13:05.872Z

Example Code: Use the date.toISOString() Method to Convert the Specified Date and Time Into the ISO Format

The format of writing the date and time can be different. Hence, we have to use the .toISOString() method to convert the given date into an internationally accepted format.

In this example, we have created two date objects using different formats. We will use the date.toISOString() method to convert the specified dates into the ISO format.

var date=new Date(2022, 7, 15, 20, 22, 10);
let iso = date.toISOString();
console.log(iso);
var dateobj = new Date('October 15, 2022 05:35:32');
let isostring = dateobj.toISOString();
console.log(isostring);

Output:

2022-08-16T03:22:10.000Z
2022-10-15T12:35:32.000Z

The date.toISOString() method is supported on most browsers. Users can get a string with the date and time in ISO format using the .toISOString() method.

Author: Shubham Vora
Shubham Vora avatar Shubham Vora avatar

Shubham is a software developer interested in learning and writing about various technologies. He loves to help people by sharing vast knowledge about modern technologies via different platforms such as the DelftStack.com website.

LinkedIn GitHub

Related Article - JavaScript Date