简体   繁体   中英

Angular store date value in localStorage

Hey I would like to store time of login to the 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()

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. In that case add .getTime() for convert time into linux timestamp format, and than add .toString() for convert timestamp into string format.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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