I have a moment.js Date object that I work with that I want to persist in redux
and redux-persist
and then retrieve the Date object from the redux-persist
store and work with it again as a moment.js Date object with all of the accompanying functions that come with moment.js Date object.
Currently whenever the moment.js Date object is retrieved from the redux-persist
store it loses all of the functions that should accompany it.
Therefore now I have 2 options on how to continue (as far as I can see):
1.) Stringify the moment.js Date object when saving to redux-persist
store and parse the moment.js Date string back to an object when retrieving from the redux-persist
store.
JSON.stringify does not stringify functions. However I found a npm package that can stringify functions as JSON: https://www.npmjs.com/package/json-fn
So my first question is where in the redux code can I insert the code that says if you are about to save to the redux store then stringify the moment.js Date object with its functions??
Then on the other side of the coin....where in the redux code can I insert the code that says when retrieving from the AsyncStorage that redux-persist uses then parse the perviously stringified object and parse it back as a moment.js object ???
2.) Re-initialise a moment.js Date object from the string I get after retrieving the data from the redux-persist store. In my case the string I'm getting back is "1981-02-27T22:13:41.000Z" How do I parse a String back to a moment.js Object? The docs does not provide a exact equivalent for "1981-02-27T22:13:41.000Z" when parsing. This is the docs: https://momentjs.com/docs/#/parsing/string/
Is it moment("1981-02-27T22:13:41.000Z", "YYYY MM DD hh:mm:ss Z", true);
or is it moment("1981-02-27T22:13:41.000Z", "YYYY MM DDhh:mm:ssZ", true);
or are both wrong in which case what is then the proper way to parse a string like "1981-02-27T22:13:41.000Z" into a moment.js object?