The toISOString method in JavaScript provides a string representation of a Date object. The string produced adheres to the simplified extended ISO format and consistently contains either 24 or 27 characters.
Syntax
The syntax for the toISOString method is illustrated as follows:
Example
dateObj.toISOString()
Return
A Date object in the form of ISO string.
JavaScript Date toISOString method example
In this section, we will explore the toISOString method by examining a variety of examples.
Example 1
Let us examine a scenario where we retrieve the current date formatted as an ISO string.
Example
<script>
var date=new Date();
document.writeln(date.toISOString());
</script>
Output:
Output
2018-08-09T13:01:38.116Z
Example 2
Let's examine an example that demonstrates how to transform a given Date object into the format of an ISO string.
Example
<script>
var date=new Date(1947, 7, 15, 20, 22, 10);
document.writeln(date.toISOString());
</script>
Output:
Output
1947-08-15T14:52:10.000Z