Convert Json Date to Javascript Date

Convert Json Date to Javascript Date

Added by Nasir Mahmood updated on Tuesday, November 11, 2014

Problem:

According to Wiki

JavaScript syntax defines several native data types that are not included in the JSON standard:[13] Date, Error, Regular Expression, and Function. These JavaScript data types must be represented by some other data format, with the programs on both ends agreeing on how to convert between the types. As of 2011, there are some de facto standards, e.g., converting from Date to String, but none universally recognized.[14][15] Other languages may have a different set of native types that must be serialized carefully to deal with this type of conversion

... and from Dates and JSON

The new format is "/Date(1404618284153)/" where the number is again the number of milliseconds since January 1st 1970 UTC. I would gladly agree that this is still not super readable, which could be solved by using ISO 8601 instead.

Solution:

This is small snippet which converts json date to javascript object.

var datestr='/Date(1404618284153)/';
var dateobject=new Date(parseInt(datestr.substr(6)))
				

Explanation:

Substring function of string will remove /Date( now we get 1404618284153)/. Now )/ is ignored by parseInt function. At last javascript Date function receive Date(1404618284153) which is well recognize by javascript.

Related Tags

About

29 Tutorials
25 Snippets
6 Products

More

Contact Us

Contact us

Stay Connected