This guide demonstrates the usage of the “Date.toJSON()” method in JavaScript.
How to Use the “Date to JSON()” Method in JavaScript?
The “Date.toJSON()” method converts the current/particular Date into a string format i.e., JSON date. It returns the date and time separated by a literal “T” alongside the “UTC” represented by “Z”( based on a worldwide 24-hour clock).
Syntax
In the above syntax:
- Dateobj: It represents the date object that needs to be converted into JSON formatting.
- toJSON: It stands for “JavaScript Object Notation” that returns the particular date.
Let’s implement this syntax practically.
HTML Code
First, overview of the following HTML code:
In the above lines of code:
- Define a subheading using the “<h2>” tag.
- Next, create an empty paragraph via the “<p>” tag having an assigned id “para” to display the output of the JavaScript code i.e., “Date” in string format.
Note: The above HTML code is followed throughout the article.
Example 1: Applying the “Date to JSON()” Method to Convert the Current Date to JSON
In this example, the “Date.toJSON()” method is used to transform the current date into the string format.
JavaScript Code
Have a look at the below-stated JavaScript code:
var dobj = new Date();
var result = dobj.toJSON();
document.getElementById("para").innerHTML = result;
</script>
In the above lines of code:
- Create a Date object using the “new” keyword and the ”Date()” constructor, respectively that returns the current date and time.
- After that, associate the “toJSON()” method with the created object to convert the created Date object into a string.
- Lastly, the “getElementById()” fetches the paragraph via its id “para” for displaying the converted Date into a string as a paragraph.
Output
The output displays the current date and time in a string format by following the “YYYY-MM-DDTHH:mm:ss.sssZ” format.
Example 2: Applying the “Date to JSON()” Method to Convert the Specified Date to JSON
This example applies the discussed method to convert the specified Date into a string.
JavaScript Code
Let’s go through the following JavaScript code:
var dobj = new Date('December 10, 2000 06:20:40');
var result = dobj.toJSON();
document.getElementById("para").innerHTML = result;
</script>
In the above code, the specified date and time in the “Date” object are transformed into a string using the “Date.toJSON()” method.
Output
As analyzed, the specified date is converted into a string appropriately.
Example 3: Applying the “Date to JSON()” Method With Parameters
The “toJSON()” method also responds to the random values as parameters by following the date constructor format (month, date, year, time). In this example, it is used to return the corresponding transformed string according to the passed parameters.
JavaScript Code
Consider the below-provided JavaScript code:
var dobj = new Date('3,3,5, 0:0');
var result = dobj.toJSON();
document.getElementById("para").innerHTML = result;
</script>
In the above code block, the “Date()” constructor specifies “3(month)”, “3(date)”, “5(year)”, and “0:0(time)” parameters, respectively. The associated “toJSON()” method converts it(Date) into a string and likewise, appends the outcome in the empty paragraph via the “getElementById()” method.
Output
The output shows the JSON formatted string according to the specified “Date()” constructor parameters.
Conclusion
The “Date to JSON()” method transforms the Date object contents generated by the “Date()” constructor into a string. JSON dates follow the “ISO-8601” standard time format i.e., “YYYY-MM-DDTHH:mm:ss.sssZ”. This method can be utilized to convert the current and specified Date as well as the Date with the parameters passed within the range of the time format. This guide provided all the possible ways to use the “Date to JSON()” method in JavaScript.