In my most recent personal project, I store emotion diary entries in the browser’s local storage. The local storage is a key-value store that only allows strings to be stored. So, I convert the diary entries to JSON strings. One of the properties of the diary entry object is a date. The date is converted to UTC and stored in the ISO 8601 format.

When I retrieve the diary entries from local storage, I need to parse the JSON strings into diary entry objects. I use the JSON.parse() function to parse the JSON strings. However, the date property is parsed as a string. I need to convert the string back to a date object. I do this by using an overloaded version of the JSON.parse() function that takes a reviver function as a second parameter. The reviver function is called for each property in the JSON string, so I check to see if the property is the date property and convert it back to a date object.

const diaryEntry = JSON.parse(jsonString, (key, value) => {
    if (key === 'entryDate') {
        return new Date(value);
    }
    return value;
});