Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.