Skip to content

Instantly share code, notes, and snippets.

@dotspencer
Last active November 2, 2021 09:44
Show Gist options
  • Save dotspencer/a99e004a31cbd93fa8f7828bece58708 to your computer and use it in GitHub Desktop.
Save dotspencer/a99e004a31cbd93fa8f7828bece58708 to your computer and use it in GitHub Desktop.
Javascript localStorage example with timestamp

I would suggest to store timestamp in the object you store in the localStorage

var object = {value: "value", timestamp: new Date().getTime()}
localStorage.setItem("key", JSON.stringify(object));

You can parse the object, get the timestamp and compare with the current Date, and if necessary, update the value of the object.

var object = JSON.parse(localStorage.getItem("key")),
  dateString = object.timestamp,
  now = new Date().getTime().toString();

compareTime(dateString, now); //to implement

from: http://stackoverflow.com/a/4275854/3498950

@Peter-Glo
Copy link

how can i display it in html

@sandeepnegi1996
Copy link

how can i display it in html

we can use document.getElementById("id of the elemtn").innerHTML()="what ever we wants to show "

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment