Created
March 7, 2012 13:41
-
-
Save ianoxley/1993182 to your computer and use it in GitHub Desktop.
JavaScript function to parse an ASP.NET JSON-serialised DateTime and extract the UNIX timestamp
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /** | |
| * 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