Javascript Reference - JavaScript toJSON() Method








toJSON() method return a Date object as a String, formatted as a JSON date.

JSON dates have the same format as the ISO-8601 standard: YYYY-MM-DDTHH:mm:ss.sssZ

Browser Support

toJSON() Yes 9.0 Yes Yes Yes

Syntax

dateObject.toJSON()

Parameters

None.





Return Value

return a string representing the date and time formatted as a JSON date.

Example


<!DOCTYPE html>
<html>
<body>
<button onclick="myFunction()">test</button>
<p id="demo"></p>
<script>
function myFunction() {<!--from ww  w  .j a v a2 s . co  m-->
    var d = new Date();
    var n = d.toJSON();
    document.getElementById("demo").innerHTML = n;
}
</script>
</body>
</html>

The code above is rendered as follows: