嘿,我想将登录时间存储到本地存储中。我检查了类似的主题,我认为它应该可以工作,但我遇到 typescript 问题(对我来说完全是新的)错误:“日期”类型的参数不能分配给“字符串”类型的参数。
function(a){
var date = localStorage.setItem('logTime', new Date),
date = new Date(parseInt(localStorage.getItem('logTime')));
if (value < date) { console.log("string") }
}
最佳答案
在这一行
date = new Date(parseInt(localStorage.getItem('logTime')));
您正在尝试将另一种类型的变量分配给字符串变量。
您应该将数据类型转换为字符串类型。
只需添加 .toString()
date = new Date(parseInt(localStorage.getItem('logTime'))).toString(); // Sun Mar 19 2017 23:00:06 GMT+0100 (CET)
但我看到下一步你的值(value)观是平等的。 Linux 时间戳格式中的更好的相等时间。在这种情况下,添加 .getTime()
将时间转换为 Linux 时间戳格式,然后添加 .toString()
将时间戳转换为字符串格式。
date = new Date(parseInt(localStorage.getItem('logTime'))).getTime().toString(); // 1489960817920
关于javascript - Angular 在 localStorage 中存储日期值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42892730/