Convert JSON Date into JavaScript Date

JSON is becoming very popular format to exchange data between client and server in Web 2.0 applications. Everything works very smooth apart from date in JSON. Date format in JSON is something like ‘\/Date(-606769200000-0500)\/’ where ‘-606769200000’ is number of milliseconds from January 1, 1970 and ‘-0500’ is time offset.

Now, to convert this JSON date into normal JavaScript date is nightmare. There are lot of solutions available, but none of them works perfectly.

So, finally, we come up with solution which will parse JSON date correctly considering time zone information as well.

Solution 1: (without using regular expression)

function parseJsonDate(jsonDate) { // Get the date time part var dateTimePart = jsonDate.replace(/\/+Date\(([\d+-]+)\)\/+/, '$1'); // Check for + or - sign var minusSignIndex = dateTimePart.indexOf('-'); var plusSignIndex = dateTimePart.indexOf('+'); // Get the sign index var signIndex = minusSignIndex > 0 ? minusSignIndex : plusSignIndex; // If sign index is not 0 then we are extracting offset part and if(signIndex > 0) { var datePart = dateTimePart.substring(0, signIndex); var offsetPart = dateTimePart.substring(signIndex); var offset = offsetPart.substring(0,3) + '.' + ((parseFloat(offsetPart.substring(3)) / 60) * 100).toString(); return getJSDate(parseFloat(datePart), parseFloat(offset)); } else { return getJSDate(dateTimePart, 0); } } function getJSDate(ms, offset) { // create Date object for current location var date = new Date(ms); // convert to msec // add local time zone offset // get UTC time in msec var utcTime = date.getTime() + (date.getTimezoneOffset() * 60000); // create new Date object for different city // using supplied offset var jsDate = new Date(utcTime + (3600000*offset)); return jsDate; }

Solution 2: (with the power of regular expression – Thanks to Jon)

function parseJsonDate(jsonDate) { var offset = new Date().getTimezoneOffset() * 60000; var parts = /\/Date\((-?\d+)([+-]\d{2})?(\d{2})?.*/.exec(jsonDate); if (parts[2] == undefined) parts[2] = 0; if (parts[3] == undefined) parts[3] = 0; return new Date(+parts[1] + offset + parts[2]*3600000 + parts[3]*60000); };

Now, no need of ugly solutions like passing date in string format Smile.

Happy programming!


8 thoughts on “Convert JSON Date into JavaScript Date

  1. Sudarshan, thank you man! It works very good for me, and with daylight saving in effect. In fact, I had another one that didn’t work very well and after almost a day I got you solution and it saves me. Thanks to0 much!

  2. Gangadhar Addagatla says:

    The following code solve timezone and daylight saving issues.
    var offset = (new Date().getTimezoneOffset() + 60) * 60000;

  3. Ramesh says:

    Many many thanks. I liked the one without regular expression because that is accurate. My date is 07.02.2013 14:27:02 but when i use function with regular expression and i do
    var dt = parseJsonDate(mydate)
    Then i get hours as 13 instead of 14. Not sure why it doesnt give accurate hours.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s