最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - local storage returning invalid format of date - Stack Overflow

matteradmin4PV0评论
 $localStorage.doctorDateTime.push({                            
    fullDate : new Date(doctorDateTime)
    });

I passed date string in new Date and then save it to local storage but when i retrieve it from local storage it showing me this format:

2015-01-01T13:41:18.300Z

while if console.log(doctorDateTime). it is showing right date string

 $localStorage.doctorDateTime.push({                            
    fullDate : new Date(doctorDateTime)
    });

I passed date string in new Date and then save it to local storage but when i retrieve it from local storage it showing me this format:

2015-01-01T13:41:18.300Z

while if console.log(doctorDateTime). it is showing right date string

Share Improve this question asked Jan 1, 2015 at 13:47 Muhammad Faizan KhanMuhammad Faizan Khan 10.6k21 gold badges105 silver badges197 bronze badges
Add a ment  | 

3 Answers 3

Reset to default 4

localStorage stored data are strings only. If you are trying to store something that is not string type, an implicit type coercion is taken place.

However, it looks like depending on some lib implementation you are using, because what you got behaves like Date.prototype.toISOString(), while following code behaves like Date.prototype.toString():

localStorage.setItem("fullDate", new Date(doctorDateTime));

You'd better explicitly convert the Date object to a string in your desired format before set to localStorage.

But, you could still get the Date object back with the ISO time string:

var str = '2015-01-01T13:41:18.300Z';
var time = new Date(str); // you got the object back!

That's what happens when you do ( new Date() ).toString(), it's the string representation of the date as it's converted to a string when stored in Local Storage.

Store the timestamp instead, it's a number representing milliseconds from epoch, and not an object

$localStorage.doctorDateTime.push({                            
    fullDate : ( new Date(doctorDateTime) ).getTime()
});

Local Storage's functionality is limited to handle only string key/value pairs.
Some browser will store objects, but it's not something you can rely on, you should be storing strings. The easiest would be to store the timestamp, and then run that timestamp through new Date when you get it from Local Storage to get a date object.

If you pass object to localstorage it will first do JSON.stringify of your object and then store it to local storage. So when next time you retrieve it, it will give you string value of date object. Try to do JSON.stringify(new Date()) you will have your date string. This is same string you are getting when you fetch next time.

Best solution is convert your date to timestap when you store it to local storage. And convert it to Date object when you are fetching from local storage.

LocalStorage only supports text. So it will always do JSON.stringify on your object before storing it.

Post a comment

comment list (0)

  1. No comments so far