How to convert JSON results into a date using JavaScript ? - GeeksforGeeks

31 Oct, 2019
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>
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>
Output:
Before clicking on the button:
After clicking on the button:
JavaScript-Misc
JSON
JavaScript
Web Technologies
Web technologies Questions
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Convert a string to an integer in JavaScript
Difference between var, let and const keywords in JavaScript
How to calculate the number of days between two dates in javascript?
Differences between Functional Components and Class Components in React
How to append HTML code to a div using JavaScript ?
Roadmap to Become a Web Developer in 2022
Installation of Node.js on Linux
How to fetch data from an API in ReactJS ?
Top 10 Projects For Beginners To Practice HTML and CSS Skills
Convert a string to an integer in JavaScript