The task is to convert a JSON result to JavaScript Date with the help of JavaScript. There are few two methods which are discussed below:
Approach 1:
- Use substr() method to get the integer part of string.
- Use the parseInt() method followed by Date() to get the JavaScript date.
Example: This example implements the above approach.
<!DOCTYPE HTML> <html> <head> <title> How to convert JSON results into a date using JavaScript ? </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <button onclick = "gfg_Run()"> click here </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var jsonDate = '/Date(1559083200000)/'; el_up.innerHTML = "Click on the button to convert" + " JSON result to JavaScript Date." + "<br>JSON Date - " + jsonDate; function gfg_Run() { var date = new Date(parseInt(jsonDate.substr(6))); el_down.innerHTML = date; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Approach 2:
- Use the regExp to get the integer part of string.
- Use the Date() method to get the JavaScript date.
Example: This example implements the above approach.
<!DOCTYPE HTML> <html> <head> <title> How to convert JSON results into a date using JavaScript ? </title> </head> <body style = "text-align:center;"> <h1 style = "color:green;" > GeeksForGeeks </h1> <p id = "GFG_UP" style = "font-size: 15px; font-weight: bold;"> </p> <button onclick = "gfg_Run()"> click here </button> <p id = "GFG_DOWN" style = "color:green; font-size: 20px; font-weight: bold;"> </p> <script> var el_up = document.getElementById("GFG_UP"); var el_down = document.getElementById("GFG_DOWN"); var jsonDate = '/Date(1559083200000)/'; el_up.innerHTML = "Click on the button to convert" + " JSON result to JavaScript Date." + "<br>JSON Date - " + jsonDate; function gfg_Run() { var date = new Date(jsonDate.match(/\d+/)[0] * 1); el_down.innerHTML = date; } </script> </body> </html> |
chevron_right
filter_none
Output:
-
Before clicking on the button:
-
After clicking on the button:
Recommended Posts:
- How to convert JSON string to array of JSON objects using JavaScript ?
- JavaScript | Convert an array to JSON
- How to convert PHP array to JavaScript or JSON ?
- How to Convert JS Object to JSON String in JQuery/Javascript?
- How to convert JSON data to a html table using JavaScript/jQuery ?
- How to convert a JavaScript date to UTC?
- Convert string into date using JavaScript
- How to convert date to another timezone in JavaScript ?
- How to convert milliseconds to date in JavaScript ?
- How to convert UTC date time into local date time using JavaScript ?
- How to check the input date is equal to today's date or not using JavaScript ?
- Python - Convert JSON to string
- Python | Ways to convert string to json object
- Convert Json String to Java Object Using GSON
- Convert Java Object to Json String using GSON
- Convert Java Object to Json String using Jackson API
- JavaScript JSON
- Javascript | JSON PHP
- JavaScript | JSON HTML
- JavaScript | JSON Arrays
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


