The Wayback Machine - https://web.archive.org/web/20250121114412/https://www.geeksforgeeks.org/javascript-date-toisostring-method/
Open In App

JavaScript Date toISOString() Method

Last Updated : 09 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

The date.toISOString() method is used to convert the given data object’s contents into a string in ISO format (ISO 8601) i.e. in the form of (YYYY-MM-DDTHH:mm:ss.sssZ or ÂąYYYYYY-MM-DDTHH:mm:ss.sssZ). The date object is created using the date() constructor. 

JavaScript
let dateobj = new Date('October 15, 1996 05:35:32');

let B = dateobj.toISOString();

console.log(B);

Output
1996-10-15T05:35:32.000Z

Syntax

dateObj.toISOString()

Parameters

  • This method does not take any parameters. It is just used along with a Date object created using the Date() constructor. 

Return Values

  • It returns the converted string of Date() constructor contents into ISO format (ISO 8601). 

Note: The DateObj is a valid Date object created using the Date()constructor.

Example 1: Here nothing as a parameter is passed while creating a date object but still toISOString() method returns the current day name, month name, date, year, and time. 

javascript
// Here nothing has been assigned
// while creating Date object
let dateobj = new Date();

// Contents of above date object is
// converted into a string using toISOString() method.
let B = dateobj.toISOString();

// Printing the converted string.
console.log(B);

Output:

2018-04-23T10:26:00.996Z

Example 2: Here we will pass a date object toISOString() method to return the day name, month name, date, year, and time. 

javascript
// Here nothing has been assigned
// while creating Date object
let dateobj = 
new Date('October 13, 1996 05:35:32 GMT-3:00');

// Contents of above date object is
// converted into a string using toISOString() method.
let B = dateobj.toISOString();

// Printing the converted string.
console.log(B);

Output:

1996-10-13T08:35:32.000Z

Note: Months, Dates, hours, minutes, seconds, and milliseconds should all be in their respective range of 0 to 11, 1 to 31, 0 to 23, 0 to 59, 0 to 59, 0 to 999 respectively. 

Supported Browsers

  • Google Chrome
  • Mozilla Firefox
  • Opera
  • Safari

JavaScript Date toISOString() Method- FAQs

What does the Date.prototype.toISOString() method do in JavaScript?

The Date.prototype.toISOString() method converts a date object into a string in ISO 8601 format (YYYY-MM-DDTHH:mm.sssZ).

What happens if the date is invalid?

If the Date object is invalid, toISOString() will throw a RangeError.

Can you customize the format of the string returned by toISOString()?

No, toISOString() always returns the date string in the ISO 8601 format without any customization options.

Is toISOString() locale-sensitive?

No, toISOString() outputs a locale-independent date string in the ISO 8601 format.

How does toISOString() handle time zones for dates before 1970?

toISOString() handles all dates in UTC regardless of the year, including dates before 1970.

We have a complete list of Javascript Date Objects, to check those please go through this Javascript Date Object Complete reference article.

We have a Cheat Sheet on JavaScript where we covered all the important topics of JavaScript to check those please go through Javascript Cheat Sheet-A Basic guide to JavaScript.


Next Article

Similar Reads

three90RightbarBannerImg