Javascript 保存和加载日期本地存储
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12661293/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Save and load date localstorage
提问by XCS
I have to save a date to localStorage and when the page is refreshed I want to calculate how much time has passed since then.
我必须将日期保存到 localStorage,当页面刷新时,我想计算从那时起已经过去了多少时间。
Now, here's the problem: localStorage saves the date as a string so after it is saved in localStorage trying to calculate the difference between those two dates returns NaN.
现在,问题来了:localStorage 将日期保存为字符串,因此在将日期保存在 localStorage 中之后,尝试计算这两个日期之间的差异会返回 NaN。
Try this in your javascript console:
在你的 javascript 控制台中试试这个:
var a = new Date();
var b = new Date();
console.log(b - a); //this works
localStorage.a = a;
localStorage.b = b;
console.log(localStorage.b - localStorage.a); //this doesn't work
I also tried JSON.stringify
and JSON.parse
trying to keep the date object intact, but that doesn't work either.
我也尝试JSON.stringify
并JSON.parse
试图保持日期对象完好无损,但这也不起作用。
My guess is that I have to parse the date in the localStorage. If there is not a better method, how can I do that?
我的猜测是我必须解析 localStorage 中的日期。如果没有更好的方法,我该怎么做?
回答by techfoobar
Demo:http://jsfiddle.net/AuhtS/
演示:http : //jsfiddle.net/AuhtS/
Code:
代码:
var a = new Date();
var b = new Date();
console.log(b - a); //this works
localStorage.a = a;
localStorage.b = b;
a = Date.parse(localStorage.a); // parse to date object
b = Date.parse(localStorage.b);
console.log(b - a); // now, this will work
Reason
原因
Everything is stored as a stringin localStorage
.
一切都以字符串形式存储在localStorage
.
So when you do localStorage.b - localStorage.a
, what you're attempting is trying to subtract one string from another. Which is why it doesn't work.
因此,当您这样做时localStorage.b - localStorage.a
,您尝试的是尝试从另一个字符串中减去一个字符串。这就是它不起作用的原因。
回答by Denys Séguret
To store a date in localStorage, simply do
要将日期存储在 localStorage 中,只需执行
localStorage['key'] = ''+myDate.getTime();
And to restore it :
并恢复它:
var myDate = new Date(parseInt(localStorage['key'], 10));
(you might also want to test it's defined before)
(您可能还想测试它之前定义的)
It also works with duration (a date minus another one) : Simply use the value as long (millisecondes) and convert to and from a string.
它也适用于持续时间(一个日期减去另一个日期):只需使用该值作为长(毫秒)并与字符串相互转换。
Note that JSON doesn't include a standardized date format. Don't use JSON for dates.
请注意,JSON 不包含标准化的日期格式。不要将 JSON 用于日期。
回答by Adrian May
http://rhaboo.orgis a sugar layer over localStorage that (among a lot of other things) fixes stuff like this. You'd just write:
http://rhaboo.org是 localStorage 之上的一个糖层,它(以及许多其他东西)修复了这样的东西。你只要写:
var store = Rhaboo.persistent("My Store Name");
console.log ( "Your last visit was " + store.datestamp || "never.");
store.write('datestamp', new Date());
BTW I wrote rhaboo.
顺便说一句,我写了 rhaboo。
回答by freethinker6
I'd use this:
我会用这个:
var d1 = new Date();
localStorage.setItem("key", d1.getTime());
var d2 = new Date(parseInt(localStorage.getItem[key]));
all that is missing is proper null validation.
所缺少的只是正确的空验证。
回答by Yusril Maulidan Raji
In my case, I need it in Date
type. Therefore, here it is:
就我而言,我需要它的Date
类型。因此,这里是:
var a = new Date();
localStorage.a = a;
var c = new Date(localStorage.a);
回答by Nahush Farkande
You can simply go for:
你可以简单地去:
var date1 = <date1>
var date2 = <date2>
localStorage.setItem('date1', date1.toString())
localStorage.setItem('date2', date2.toString())
var date1 = new Date(localStorage.getItem('date1'))
var date2 = new Date(localStorage.getItem('date2'))
var diff = date1 - date2