joshnuss/svelte-persisted-store

JSON serialize Date

JolloDede opened this issue · 0 comments

I have a project where i am using your library. I ran into problems when serializing and deserialising dates.
I know the JSON format doesnt have a dateformat Stackoverflow discussion. Javascript on the otherhand has a format 2012-04-23T18:25:43.511Z

Is it possible to have something that parses the date, in the js format, when calling reading from the localstorage.
Like:

dateTimeReviver = function (key, value) {
    var a;
    if (typeof value === 'string') {
        a = /\/Date\((\d*)\)\//.exec(value);
        if (a) {
            return new Date(+a[1]);
        }
    }
    return value;
}

JSON.parse(somejsonstring, dateTimeReviver);

Stackoverflow discussion

Or is there someway i am missing to get the date parsed