0

How do I go about displaying the amount of hours since the last time the user visited the webpage? I want to do this in Javascript preferably, but JQuery is fine too.

Woowing
  • 103
  • 1
  • 2
  • 15

1 Answers1

1

Assuming you have 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);
Community
  • 1
  • 1
lleaff
  • 4,249
  • 17
  • 23