5 ms·
Aha yes you are correct - it outputs an ISO string at the very least but does not parse it back to a date. I will update the article to reflect this!
by wheresvic1 8y ago
Aha yes you are correct - it outputs an ISO string at the very least but does not parse it back to a date. I will update the article to reflect this!
- simlevesque 8y agoOk but please read carefuly because JSON.parse can in fact recover dates. The JSON.parse function accepts a 'reviver' function as an argument to do just that. 99% of the time it's not useful so there is no reason for JSON.parse to do it by default but JSON.parse definitely can parse dates back if you use all it's features. I like that it does not do it by default but I can make it parse the dates if I want.
- Spivak 8y agoSaying that JSON.parse can do something when it's really just you implementing the transformation from string to date object is a little misleading. You can make JSON parse arbitrary sublanguages if you're willing to put in the work.
- dzek69 8y agoThat way you can get different result, as you could actually have date object and date as string stored somewhere. You will need custom strigify callback to differentiate which should be converted back to date on parse and which should be a string. Still, this is not pure json this way, it's your own transformations
- switch007 8y agoAnd what do you pass as the reviver argument? Your own function to check a string to see if it's valid IS8601 and parse it? That's just using parse()'s recursive walking of the object: it does no date reviving itself, right?