Date. toJSON() function in JavaScript


The Date object is a data type built into the JavaScript language. Date objects are created with the new Date( ) as shown below.

Once a Date object is created, a number of methods allow you to operate on it. Most methods simply allow you to get and set the year, month, day, hour, minute, second, and millisecond fields of the object, using either local time or UTC (universal, or GMT) time.

The toJSON() function of the date object returns the simplified extended ISO format of the date.

Syntax

Its Syntax is as follows

dateObj.toJSON();

Example

 Live Demo

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj = new Date('september 26, 89 12:4:25:96');
      var jsonDate = dateObj.toJSON();
      document.write("Current Date: "+jsonDate);
   </script>
</body>
</html>

Output

Current Date: 1989-09-26T06:34:25.096Z

Example

If you do not pass parameters to the Date constructor this function returns the JSON containing the current time and date.

 Live Demo

<html>
<head>
   <title>JavaScript Example</title>
</head>
<body>
   <script type="text/javascript">
      var dateObj = new Date('september 26, 89 12:4:25:96');
      var jsonDate = dateObj.toJSON();
      document.write("Current Date: "+jsonDate);
   </script>
</body>
</html>

Output

Current Date: 2018-10-18T11:57:45.116Z

Samual Sam
Samual Sam

Learning faster. Every day.

Updated on: 25-Jun-2020

162 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements