Save date and get from local storage

I saved the date for local storage as below.

JS:

var currentTime = new Date(); //get the current time.

//Clock in the time.
localStorage.time = currentTime;

When I try to get it later using ...

var timeObj = new Date(localStorage.time);
var checkInDayOfMonth = timeObj.getUTCDate(); //returns 1-31 

timeObj will not have the correct datetime, instead it will have the current time, as if it were ignoring the parameters of the time that I am sending.

I use getUTCDate to get the day of the month. If today's value is different from what is in the repository, I know this is a new day.

Opening the Google Chrome Inspector shows the date stored in localStorage in this format:

Wed Dec 11 2013 22:17:45 GMT-0800 (PST)

Is this not an acceptable format for a date constructor?

How can I properly store and return dates from localStorage?

+4
3

unix. Date.

. + , .

localStorage.setItem('time', +new Date);

, Date:

new Date(parseInt(localStorage.getItem('time')));
+10

UNIX :

window.localStorage.time = new Date().getTime();

var date = new Date(parseInt(window.localStorage.time));
+2

Try the following:

var currentTimeStr = timeObj.getDate() + "-" + (timeObj.getMonth()+1) + "-" + timeObj.getUTCFullYear() + " " + timeObj.getHours() + ":" + timeObj.getMinutes();

He gave me the conclusion: "12-12-2013 13:44" (I received at 13:51, so he did not give the current time.)

Hope this helps.

0
source

All Articles