I need to save the date in localStorage, and when the page refreshes, I want to calculate how much time has passed since then.
Now here's the problem: localStorage saves the date as a string, so after saving it to localStorage trying to calculate the difference between the two dates, returns NaN.
Try this in your javascript console:
var a = new Date(); var b = new Date(); console.log(b - a);
I also tried JSON.stringify and JSON.parse , trying to keep the date object intact, but that doesn't work either.
I assume that I need to parse the date in localStorage. If there is no better method, how can I do this?
Cristy
source share