Last active
April 19, 2018 14:33
-
-
Save mariusschulz/1d5f6348def8d94cd8eef6d3070178be to your computer and use it in GitHub Desktop.
Automatic JSON date deserialization
This file contains 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
const DATE_PATTERN = /^\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}\.\d+$/; | |
function deserializeProperty(key, value) { | |
return typeof value === "string" && DATE_PATTERN.test(value) | |
? new Date(value) | |
: value; | |
} | |
const obj = JSON.parse('{ "date": "2016-04-26T18:09:16.61" }', deserializeProperty); | |
console.log(typeof obj.date); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment