How to convert a date object's content into json in JavaScript?


The contents of the specified date object are converted into a string using the date.toJSON() function. Applying date() constructor, the date object is generated. Dates describe a particular moment in time. The value of the Date object is described by a string that is returned by the toJSON() function (using toISOString()). In general, this function is programmed to automatically meaningfully serialise Date objects during JSON serialisation.

The DateObj is a valid Date object produced by the Date() constructor function, and its data has been transformed into a string. Here are some additional codes for the procedure mentioned below in the examples −

Syntax

Following is the syntax of date object's into json

dateObj.toJSON()

Parameters − There are no parameters that this method accepts. It is only used with a Date object formed with Date ()constructor

Return Value

A string that displays the specified date.

Example 1

In this example let us understand the basic example of Date toJSON() method.

<!DOCTYPE html> <html> <title>How to convert a date object's content into json in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <div id="result"></div> <script> let customDate = new Date('December 31, 2002 05:35:32'); let stringJson = customDate.toJSON(); document.getElementById("result").innerHTML =stringJson; </script> </body>

Example 2

The toJSON() method still returns the current day name, month name, date, year, and time even though no parameters are supplied when generating the date object in this case.

<!DOCTYPE html> <html> <title>How to convert a date object's content into json in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <div id="result"></div> <script> let customDate = new Date(); let stringJson = customDate.toJSON(); document.getElementById("result").innerHTML =stringJson ; </script> </body> </html>

Example 3

The toJSON() method returns the corresponding generated string when a seemingly random list of values is supplied. Date() constructor's format is similar to Date (month, date, year, time). By using this format, some values are sent to the software below, and the corresponding string is generated as output. the following time format is appropriate (number:number:number).

<!DOCTYPE html> <html> <title>How to convert a date object's content into json in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <script> let dateObject1 = new Date('1'); let dateObject2 = new Date('2, 3,4'); let dateObject3 = new Date('2, 3, 4, 11:00:12'); let dateObject4 = new Date('12, 5, 4, 0:0');let str1 = dateObject1.toJSON();
let str2 = dateObject2.toJSON(); let str3 = dateObject3.toJSON(); let str4 = dateObject4.toJSON();
document.write(str1 + "<br>"); document.write(str2 + "<br>"); document.write(str3 + "<br>"); document.write(str4); </script> </body> </html>

Example 4

Months, Date, hour, minute, second, and millisecond must fall within the boundary of Zero to 11 for months, One to 31 for date, Zero to 23 for hours, Zero to 59 for minutes, Zero to 59 for seconds, and Zero to 999 for milliseconds in order for the toJSON() method will return null.

Here date given as of 42 which is out of range of date that is why the below code gives the output as null.

<!DOCTYPE html> <html> <title>How to convert a date object's content into json in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <script> let customDateObj = new Date('October 42, 1996 05:35:32'); let strJson = customDateObj.toJSON(); document.write(strJson); </script> </body> </html>

Example 5

In this example let us understand how to retrieve the current date as a string.

<!DOCTYPE html> <html> <title>How to convert a date object's content into json in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <script> let date=new Date(); let strJson=date.toJSON(); document.writeln(new Date(strJson).toUTCString()); </script> </body> </html>

Example 6

We could see that we just want the first 10 characters of the date, so let's use JavaScript's slice() technique. In our example, the method slice(a, b) slices the string from 0 to 10 between points a and b. Let's give it a shot −

This string can be divided even more so that the bits can be assigned to the variables day, month, and year, but doing so would be excessive. If you require a different format, simply choose a different path.

<!DOCTYPE html> <html> <title>How to convert a date object's content into json in JavaScript - TutorialsPoint</title> <head> <meta charset="UTF-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> </head> <body> <script> let date = new Date().toJSON().slice(0, 10); document.write(date); </script> </body> </html>

Updated on: 04-Aug-2022

913 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements