Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
JavaScript function to parse an ASP.NET JSON-serialised DateTime and extract the UNIX timestamp
/**
* Returns the UNIX timestamp from the ASP.NET JSON-serialised DateTime
*
* @param d - a string in the format /Date(1331127585489)/
* @return UNIX timestamp extracted from d, or zero
* @see http://www.hanselman.com/blog/OnTheNightmareThatIsJSONDatesPlusJSONNETAndASPNETWebAPI.aspx
* for possible changes to the way DateTime objects are serialised to JSON
*
* Example usage:
* var jsonDateTime = '/Date(1331127585489)/';
* console.log(new Date(parseUnixTime(jsonDateTime)));
*/
function parseUnixTime(d) {
var tmp = d.match(/\d+/);
if (tmp && tmp.length) {
return parseInt(tmp[0]);
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment