Home > Web Front-end > JS Tutorial > How to convert JSON result to date using JavaScript?

How to convert JSON result to date using JavaScript?

王林
Release: 2023-09-08 11:33:08
forward
618 people have browsed it

如何使用 JavaScript 将 JSON 结果转换为日期?

JSON is a powerful data format used for exchanging data between servers and clients. Many times, JSON data is received in string format and we need to convert it into a usable JSON object. In this process, an important requirement is to convert string data into Date format. In this article, we will learn how to convert JSON results into date string using Javascript.

The JSON object contains a date like this -

{
   name: "John",
   time: '/Date(1559072200000)/'
}
Copy after login

The result will be -

Wed May 29 2019 01:06:40 GMT+0530 (India Standard Time) 
Copy after login

Here are some ways to achieve this -

  • Use string.replace method

  • Use regular expressions

Method 1: Use String’s replace() method

The replace method in JavaScript is used to replace part of a string with another string. Following are the steps to convert JSON result to date using String.replace method.

  • Replace the first part of the string "/Date(" with the empty string

  • Replace the last part of the string ")/" with the empty string

  • Create a new Date object by parsing the milliseconds in a JSON string

  • Now that you have the date, you can use it as a normal javascript date.

Example

In this example, we use the String.replace() method to convert the JSON result into a date.

<html> 
<body>
   <h2>Convert JSON results into a date using JavaScript</h2>
   <p>Click the following button to convert JSON results into a date</p>
   <button id="btn" onclick="convert( )"> Click Here </button> <br>
   <h3>Input Data : </h3>
   <p id="input"> /Date(1559072200000)/ </p>
   <h3> Resulting Date: </h3>
   <p id="output"> </p>
   <script>
      function convert() {
         
         // Store the JSON date string in a variable
         var jsonDate = '/Date(1559072200000)/';
         
         // Replace the first part of the string "/Date(" with an empty string
         jsonDate = jsonDate.replace("/Date(", " ")
         
         // Replace the last part of the string ")/" with an empty string
         jsonDate = jsonDate.replace(")/", " ")
         
         // Create a new Date object by parsing the number of milliseconds from the JSON string
         let strDate = new Date(parseInt(jsonDate));
         
         // Get the and output element in the HTML document
         let output = document.getElementById("output")
         
         // Set the inner text of the output element to the formatted date
         output.innerText = strDate;
      }
   </script>
</body>
</html>
Copy after login

Method 2: Use regular expressions

The following are the steps to convert JSON results to dates using regular expressions.

  • Extract unix timestamp from JSON date string using regular expressions

  • Create a new Date object by parsing the milliseconds in a JSON string

  • Now that you have a date, you can use it as a normal JavaScript date.

<html>
<body>
   <h2>Convert JSON results into a date using JavaScript</h2>
   <p>Click the following button to convert JSON results into a date</p>
   <button id="btn" onclick="convert( )"> Click Here </button> <br>
   <h3>Input Data : </h3>
   <p id="input"> /Date(1559072200000)/ </p>
   <h3> Resulting Date: </h3>
   <p id="output"> </p>
   <script>
      
      // Function to convert the JSON date format to a readable date
      function convert() {
         
         // The JSON date string in the format '/Date(unixTimestamp)/'
         var jsonDate = '/Date(1559072200000)/'; 
         
         // Extract the Unix timestamp from the JSON date string using regex
         jsonDate = jsonDate.match(/\d+/);
         
         // Create a new Date object using the unix timestamp
         let strDate = new Date(parseInt(jsonDate));
         
         // Get a reference to the HTML element with the id "output"
         let output = document.getElementById("output");
         output.innerText = strDate;
      }
   </script>
</body>
</html>
Copy after login

The above is the detailed content of How to convert JSON result to date using JavaScript?. For more information, please follow other related articles on the PHP Chinese website!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template