Friday, September 5, 2025
HomeLanguagesJavascriptHow to convert JSON results into a date using JavaScript ?

How to convert JSON results into a date using JavaScript ?

The task is to convert a JSON result to JavaScript Date with the help of JavaScript. There are two methods which are discussed below: 

Approach 1:

Example: This example implements the above approach. 

html




<h1 style="color:green;">
    neveropen
</h1>
  
<p id="GFG_UP">
</p>
  
<button onclick="gfg_Run()">
    click here
</button>
  
<p id="GFG_DOWN">
</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>


Output:

How to convert JSON results into a date using JavaScript ?

How to convert JSON results into a date using JavaScript ?

Approach 2:

  • Use the regExp to get the integer part of the string.
  • Use the Date() method to get the JavaScript date.

Example: This example implements the above approach. 

html




<h1 style="color:green;">
    neveropen
</h1>
  
<p id="GFG_UP">
</p>
  
<button onclick="gfg_Run()">
    click here
</button>
  
<p id="GFG_DOWN">
</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>


Output:

How to convert JSON results into a date using JavaScript ?

How to convert JSON results into a date using JavaScript ?

RELATED ARTICLES

Most Popular

Dominic
32269 POSTS0 COMMENTS
Milvus
81 POSTS0 COMMENTS
Nango Kala
6638 POSTS0 COMMENTS
Nicole Veronica
11802 POSTS0 COMMENTS
Nokonwaba Nkukhwana
11868 POSTS0 COMMENTS
Shaida Kate Naidoo
6752 POSTS0 COMMENTS
Ted Musemwa
7029 POSTS0 COMMENTS
Thapelo Manthata
6704 POSTS0 COMMENTS
Umr Jansen
6721 POSTS0 COMMENTS