Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

local time doesnt save in local storage after page refresh

Im quite a newbie with coding

So i got the code below, the local time is being set correctly on the webpage and in local storage. However, after refreshing the time sets again and i dont need that. I was looking through several topics and couldnt find anything that fits my problem.
Tried document.write() etc, but didnt work

What could be the solution to this problem or what are the issues in my code that the time sets new after refresh?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const date = new Date().toLocaleTimeString();
document.getElementById('entryTime').textContent = date


localStorage.setItem('time', JSON.stringify(date));

if (localStorage.getItem("time")) {
  entryTime = JSON.parse(localStorage.getItem("time"));
} else {
  // No data, start with an empty array
  entryTime = [];
}
console.log(date);
document.getElementById('entryTime').innerHTML = date
<div class="row" style="margin-top: 10px !important; margin-bottom: 1px !important; font-size: 30px;" id="entryTime"></div>

>Solution :

Does this work for you?

var entryTime;
if (localStorage.getItem("time")) {
    entryTime = JSON.parse(localStorage.getItem("time"));
} else {
    entryTime  = new Date().toLocaleTimeString();
    localStorage.setItem('time', JSON.stringify(entryTime ));
}
document.getElementById('entryTime').innerHTML = entryTime;
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading