I have been assigned to display to the user the number of days since their last visit to my website and have to use the "localStorage" web api. I believe I have a good start, and I know that I will need to access and store the last visit date and get the difference between the current date of their visit. I am just unsure of how to access the date of their last visit.
Here is my code:
HTML:
<article>Days since your last visit:<span class="timeBetween"></span></article>
The class name of the span I want to display to is "timeBetween"
JavaScript:
const visitsDisplay = document.querySelector('.timeBetween');
let numVisits = Number(window.localStorage.getItem("timeBetween"));
//This is where I believe I need to get the days between visits
//Something like:
const visitDifference = "pastvisit" - Date.now();
localStorage.setItem("timeBetween", visitDifference);
3
Answers
I think the flow should be like below:
you should save the current Time when user opens your site every time
and where you are displaying the difference of last visited time and current time
Since you want to calculate the number of days between the visitor’s last visit, you could use something like this (could be polished and improved, but it works)