如何获取自上次访问网页以来的小时数 Javascript

How To Get Hours Since Last Webpage Visit In Javascript

如何显示自用户上次访问网页以来的小时数?我最好在 Javascript 中执行此操作,但 JQuery 也可以。

假设你有 getCookie and setCookie functions:

const lastVisit = getCookie('lastVisitTime');
const now = Date.now();
if (lastVisit) {
   const hoursSinceLastTime = Math.ceil((parseInt(lastVisit) - now) / 3600);
   alert(`It's been ${hoursSinceLastTime} hour(s) since you last visited us.`);
}
setCookie('lastVisitTime', now);