In JavaScript how do I save a Date object (10 minutes in future) to localStorage, then later check local storage for that date object to see if NOW > dateObject
function getDateObject10MinutesInFuture() {
return new Date( Date.now() + (1000 * 60 * 10) );
}
function saveDateObjectToLocalStorage(dateObject, key) {
//what here
}
function isDateObjectExpiredInLocalStorage(key) {
//what here
}
I think the best solution that I've found is to convert the date object to a string:
const dateInFuture = new Date( Date.now() + (1000 * 60 * 10) );
const dateInFutureStr = date.toString();
// then save the string form in local storage
localStorage.setItem('date', dateInFutureStr);
//then get the string form from local storage on another page or wherever
const checkDateStr = localStorage.getItem('date');
const checkDate = new Date(checkDateStr);
if (new Date() > checkDate) {
console.log('date expired');
}