Method
Introduction to JavaScript Date toISOString() Method
The Date.toISOString() method in JavaScript is used to get the date in ISO format (ISO 8601) i.e. in the form of a string. It returns the date in the form of YYYY-MM-DDTHH:mm:ss.sssZ where ‘T’ is a separator between the date and time and ‘Z’ is the time zone.
Syntax
The syntax for the JavaScript Date.toISOString() method is as follows:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
dateObj.toISOString()
[/dm_code_snippet]
Here, dateObj
is the object of Date
class.
Return Value
The JavaScript Date.toISOString() method returns the date in the form of a string according to the ISO 8601 standard in the form of YYYY-MM-DDTHH:mm:ss.sssZ.
Example
Let’s look at an example of using the Date.toISOString() method.
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
//Creating a date object
var date = new Date(2020, 11, 25, 10, 33, 30, 0);
//Printing the date object
console.log(date);
//Printing the date in ISO format
console.log(date.toISOString());
[/dm_code_snippet]
Output:
[dm_code_snippet background=”yes” background-mobile=”yes” slim=”no” line-numbers=”no” bg-color=”#abb8c3″ theme=”dark” language=”php” wrapped=”no” height=”” copy-text=”Copy Code” copy-confirmed=”Copied”]
2020-12-25T10:33:30.000Z
[/dm_code_snippet]
Here, we created a Date
object and printed it in the ISO format by using the toISOString() method.
Browser Support
The Date.toISOString() method is supported in the following browsers:
- Chrome
- Internet Explorer
- Firefox
- Opera
- Safari
Conclusion
In this tutorial, we discussed the JavaScript Date.toISOString() method and saw its syntax, return value, and example. We also saw the browser support for this method.