Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
How to convert JSON results into a date using JavaScript?
JSON is a powerful data format to exchange data from server to client and vice versa. Many time JSON data is received in a String format and we need to convert it to a usable JSON object. In this process, it's an important requirement to convert string data into Date format. In this article, we will learn how to convert JSON results into a Date string using Javascript.
The JSON objects contain the date like this ?
{
name: "John",
time: '/Date(1559072200000)/'
}
And the result will be ?
Wed May 29 2019 01:06:40 GMT+0530 (India Standard Time)
Here are a few approaches to achieve this ?
Using string.replace method
Using Regex
Approach 1: Using String replace( ) Method
The replace method in JavaScript is used to replace a portion of a string with another string. Here are the steps to convert JSON results into a date using the String.replace method.
Replace the first part of the string "/Date(" with an empty string
Replace the last part of the string ")/" with an empty string
Create a new Date object by parsing the number of milliseconds from the JSON string
Now you got the Date and you can use it as a normal javascript date.
Example
In this example we are converting JSON results into a date using the String.replace() method.
<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>
Approach 2: Using Regex
Here are the following steps to convert JSON results into a date using regex.
Extract the unix timestamp from the JSON date string using regex
Create a new Date object by parsing the number of milliseconds from the JSON string
Now you got the Date and 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>