JavaScript toISOString(): Get the date as a string based on the ISO Standard

The JavaScript toISOString() method is used to get the date as a string based on the ISO (International Organization for Standardization) standard. For example:

HTML with JavaScript Code
<!DOCTYPE html>
<html>
<body>
   
   <p id="xyz"></p>

   <script>
      const d = new Date();
      let dateISO = d.toISOString();
      document.getElementById("xyz").innerHTML = dateISO;
   </script>

</body>
</html>
Output

This code creates a paragraph element with the ID "xyz" and then uses JavaScript to set its text content to a string representing the current date and time in ISO format.

The new Date() function returns a new Date object with the current date and time. The date is then converted to an ISO formatted string using the toISOString() method on this object. The dateISO variable is then assigned this ISO formatted string.

Finally, the innerHTML property is used to set the text content of the paragraph element with ID "xyz" to the dateISO value.

The output will show the current date and time in ISO format, such as "2023-04-24T05:17:41.259Z".

Please note: The ISO date format is YYYY-MM-DDTHH:mm:ss.sssZ, where:

JavaScript toISOString() syntax

The syntax of the toISOString() method in JavaScript is:

x.toISOString()

where x must be an object of the Date() constructor.

The toISOString() method returns a string representing both the date and the time based on the ISO standard.

JavaScript Online Test


« Previous Tutorial Next Tutorial »


Liked this post? Share it!