How to Convert JSON Date Format to Javascript Date

When getting data from a REST service, it is common to serialize the data as JSON string. Parsing the data returned into a javascript object is easy with a simple eval. But when the data returned includes a date, then the JSON date format usually looks like

12
/Date(/Date(1426636800000)/)/

To convert this date to the standard date format for display to the frontend, we first need to extract the date part as below:

123456
var d = "/Date(1426636800000)/";
var datePart = parseInt(d.substr(6));

//The result for the above looks like
//1426636800000

To further convert this into the standard date formatting, you can simply use the Javascript’s toLocaleDateString() method [if the date is only to display as a string to the frontend]

1234
datePart.toLocaleDateString("en-GB");
//The above code will return the date string as
//18/03/2015



comments powered byDisqus