r/AskProgramming • • 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

19 comments sorted by

View all comments

Show parent comment

5

u/buzzon 5h ago

If the computer and your program are guaranteed to run the entire time, you can keep data in a variable in RAM. However, when your program is restarted, all variables are lost, and you need to figure out a more permanent storage such as a file or a database.

Also, Javascript? Are you running a script in a web page in the browser? This further increases risks that the user closes your tab and the variable is lost.

-2

u/r2idw 5h ago

Ofc Im running the script in a web page, its my own project where I developing my code skills using this project, I just wanna see I get it done, cuz ive spending up 6 days trying to figure out how to solve that problem lol I ended up posting this post instead of ask ai for a help

3

u/buzzon 5h ago

Make variable 1 and put today's date into it. 

Make variable 2 and put today's date plus 7 days into it. This is the date where the calendar must be refreshed.

2

u/ALargeRubberDuck 4h ago

In that case look into storing data in localstorage. It should allow you to save the data without a backend call.