The JavaScript toISOString function is useful for returning a string representation of the given date and time in ISO 8601 simplified format. The ISO format date is either 24 or 27 characters (YYYY-MM-DDTHH:mm:ss.sssZ).
The syntax of the toISOString function is
Date. toISOString()
Return Value: The toISOString method does not accept any parameter value. It converts the given datetime into a string based on ISO format.
JavaScript toISOString example
In the following example, we use the toISOString() function to return the ISO standard string representation of today’s date and time.
const dt = new Date();
console.log(dt);
let i = dt.toISOString();
console.log(i);
Wed Jun 17 2026 10:06:36 GMT+0530 (India Standard Time)
2026-06-17T04:36:36.332Z
Example 2: In this JavaScript example, the Date() constructor parses the custom date and time. Next, the JavaScript toISOString method returns the ISO string representation of the custom date and time.
const dt = new Date('December 10, 2024 10:35:32 GMT-3:00');
console.log(dt);
console.log(dt.toISOString());
If you observe the output, the ISO date adds 3 hours to the custom date because the custom date is three hours before the ISO zone. Next, the system local date adds 8 hours 30 minutes because the system date is 5 hours 30 minutes ahead (3 + 5:30 = 8:30).
Tue Dec 10 2024 19:05:32 GMT+0530 (India Standard Time)
2024-12-10T13:35:32.000Z
Example 3: If we use the toISOString on a date without the month or day information, it considers January 1st as the default date and returns the same. Please refer to the list of date functions article.
const dt = new Date('2020');
console.log(dt);
console.log(dt.toISOString());
Wed Jan 01 2020 05:30:00 GMT+0530 (India Standard Time)
2020-01-01T00:00:00.000Z
What is the difference between toUTCString and toISOString in JavaScript?
Both the toUTCString and toISOString methods return the string representation of the given date and time.
- toUTCString follows the RFC-style string formatting, and it does not include the milliseconds in the output.
- toISOString follows the ISO 8601 format and includes milliseconds in it.
The following example shows the string formatting style and output of the toUTCString and toISOString methods.
const dt = new Date();
console.log(dt);
console.log(dt.toISOString());
console.log(dt.toUTCString());
Wed Jun 17 2026 10:19:35 GMT+0530 (India Standard Time)
2026-06-17T04:49:35.760Z
Wed, 17 Jun 2026 04:49:35 GMT