简体   繁体   English

本地存储中的角度存储日期值

[英]Angular store date value in localStorage

Hey I would like to store time of login to the localStorage. 嘿,我想存储登录到localStorage的时间。 I checked similar topics and I think it should work but I have problem with typescript (totally new for me) error : Argument of type 'Date' is not assignable to parameter of type 'string'. 我检查了类似的主题,我认为它应该可以工作,但是我遇到打字稿问题(对我来说是全新的)错误:无法将类型为“日期”的参数分配给类型为“字符串”的参数。

function(a){ 
    var date = localStorage.setItem('logTime', new Date),
        date = new Date(parseInt(localStorage.getItem('logTime')));

    if (value < date) { console.log("string") }
}

IN this line 在这条线

date = new Date(parseInt(localStorage.getItem('logTime')));

You are trying to assign to string variable another type of variable. 您正在尝试为字符串变量分配另一种类型的变量。 You should convert Data type to string type. 您应该将数据类型转换为字符串类型。 Just add .toString() 只需添加.toString()

date = new Date(parseInt(localStorage.getItem('logTime'))).toString(); // Sun Mar 19 2017 23:00:06 GMT+0100 (CET)

But I see in next step you equal values. 但是我下一步看到您等于价值。 Better equal time in linux timestamp format. linux时间戳格式的平等时间更好。 In that case add .getTime() for convert time into linux timestamp format, and than add .toString() for convert timestamp into string format. 在那种情况下,添加.getTime()可以将时间转换为Linux时间戳格式,然后添加.toString()可以将时间戳转换为字符串格式。

date = new Date(parseInt(localStorage.getItem('logTime'))).getTime().toString(); // 1489960817920

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM