Skip to content

Instantly share code, notes, and snippets.

@mariusschulz
Last active April 19, 2018 14:33
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mariusschulz/1d5f6348def8d94cd8eef6d3070178be to your computer and use it in GitHub Desktop.
Save mariusschulz/1d5f6348def8d94cd8eef6d3070178be to your computer and use it in GitHub Desktop.
Automatic JSON date deserialization
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