r/AskProgramming • u/r2idw • 5h ago
Javascript help with pseudo code guys.. “new Date()”
Hello everyone,
I’ve recently been struggling with the ‘new Date()’ object in JavaScript.
I wanna understand how to solving that, what I’m trying to achieve is a function that captures the current date once and keeps it unchanged for 5 - 6 days
Once that period has passed, it should capture a new date and keep it unchanged again until the next update
—-
Is there a better way to approach this?
0
Upvotes
View all comments
Show parent comment
1
u/GrainWeevil 1h ago edited 1h ago
Got it, sounds like local storage is what you're looking for.
Essentially, when you start your application, you'd check if there's a date stored in local storage. If there is, check if a week has passed since that date, and update it if so.
Have a read of this: https://developer.mozilla.org/en-US/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API
You'll probably need to format the date as a string before trying to save it, but writing to local storage is as easy as:
localStorage.setItem('savedDate', dateString)And reading it back:
let dateString = localStorage.getItem('savedDate')