The Wayback Machine - https://web.archive.org/web/20211124113358/https://www.geeksforgeeks.org/javascript-date-toisostring-method/
Skip to content
Related Articles

Related Articles

Save Article
Improve Article
Save Article
Like Article

JavaScript Date toISOString() Method

  • Last Updated : 26 Oct, 2021

Below is the example of Date toISOString() method.

Hey geek! The constant emerging technologies in the world of web development always keeps the excitement for this subject through the roof. But before you tackle the big projects, we suggest you start by learning the basics. Kickstart your web development journey by learning JS concepts with our JavaScript Course. Now at it's lowest price ever!

  • Example:




    <script>
       // Here a date has been assigned
       // while creating Date object
       var dateobj = 
       new Date('October 15, 1996 05:35:32');
      
       // Contents of above date object is converted
       // into a string using toISOString() function.
       var B = dateobj.toISOString();
      
       // Printing the converted string.
       document.write(B);
    </script>
  • Output:
    1996-10-15T00:05:32.000Z

The date.toISOString() method is used to convert the given date 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 date() constructor.

Syntax:

dateObj.toISOString()

Parameters: This method does not take any parameter. It is just used along with a Date object created using 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 Date() conctructor.
More codes for the above method are as follows:

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




<script>
   // Here nothing has been assigned
   // while creating Date object
   var dateobj = new Date();
  
   // Contents of above date object is
   // converted into a string using toISOString() method.
   var B = dateobj.toISOString();
  
   // Printing the converted string.
   document.write(B);
</script>

Output:

2018-04-23T10:26:00.996Z

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




<script>
    // Here nothing has been assigned
    // while creating Date object
    var 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.
    var B = dateobj.toISOString();
  
    // Printing the converted string.
    document.write(B);
</script>                    

Output:

1996-10-13T08:35:32.000Z

Note: Months, Dates, hours, minutes, seconds, 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: The browsers supported by JavaScript Date toISOString() method are listed below:

  • Google Chrome 3 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 9 and above
  • Opera 10.5 and above
  • Safari 5 and above

JavaScript is best known for web page development but it is also used in a variety of non-browser environments. You can learn JavaScript from the ground up by following this JavaScript Tutorial and JavaScript Examples.




My Personal Notes arrow_drop_up
Recommended Articles
Page :