我正在尝试检查本地存储中的时间戳是否超过 6 小时,我认为我的逻辑不正确。这是我目前所拥有的。
function checkBasket() {
const basket = localStorage.getItem('user_basket');
if (basket) {
var sixHours = 5 * 60 * 60 * 1000;
return ((new Date) - JSON.parse(basket).timestamp) < sixHours;
}
return false;
}
因此,如果 localstorage basket.timestamp 小于 6 小时,我试图让此函数返回 true,否则返回 false。
最佳答案
var date1 = new Date("Mon Oct 24 2016 04:22:12 GMT+0530 (IST)");
var date2 = new Date("Mon Oct 24 2016 10:22:12 GMT+0530 (IST)");
var hours = Math.floor(Math.abs(date1 - date2) / 36e5);
console.log(hours);
您可以使用此代码片段
引用: How to get the hours difference between two date objects?
关于Javascript,检查时间戳是否从现在开始超过 6 小时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40209977/