Home >>JavaScript Date Object Methods >JavaScript toJSON() Method

JavaScript toJSON() Method

JavaScript toJSON() Method

JavaScript toJSON() method is used to convert a given Date object into a string formatted as a JSON date. The JSON dates have the same format as the ISO-8601 standard: YYYY-MM-DDTHH:mm:ss.sssZ.

Syntax:
Date.toJSON()

Browser Support

Method Chrome Edge Firefox Safari Opera
toJSON() Yes 9 Yes Yes Yes
Here is an example of JavaScript toJSON() method:
<html>
<body>
<p>Click the button to see the Output.</p>
<button onclick="myFunction()">Try it</button>
<p id="demo"></p>
<script>
function myFunction() 
{
  var d = new Date();
  var n = d.toJSON();
  document.getElementById("demo").innerHTML = n;
}
</script>
</body>
</html>
Output:

Click the button to see the Output.


No Sidebar ads