Created
January 28, 2012 05:17
-
-
Save jcbozonier/1692807 to your computer and use it in GitHub Desktop.
Using a date reviver in JSON.parse
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
function dateReviver(key, value) { | |
if (typeof value === 'string') { | |
var a = /^(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}(?:\.\d*)?)Z$/.exec(value); | |
if (a) { | |
return new Date(Date.UTC(+a[1], +a[2] - 1, +a[3], +a[4], +a[5], +a[6])); | |
} | |
} | |
return value; | |
}; | |
JSON.parse(serialized_object, dateReviver); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Note this doesn't revive dates that have an offset. Ex.
2018-07-20:08:00:00-04:00
🤓Solution: