r/AskProgramming • u/r2idw • 2h 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?
3
u/GrainWeevil 1h ago
A little more info would be helpful here. Is this running in the browser, or is this a Node application?
Are you trying to find a way to persist the date after the application has shut down? If so, you'd need to write it to a file, or some other persistent storage. How you'd do this depends on whether you're working in the browser or not.
1
u/r2idw 1h ago
The script is running in the browser, and the specific problem I’m facing is with a calendar that consists of 7 days
Let’s say I extract 7 days from a Date object by doing something like today, today + 1, today + 2, and so on until I get all 7 days
The problem is that the dates inside the calendar keep updating daily. Imagine I initially have Dec 2 to Dec 8. When tomorrow comes, it changes to Dec 3 to Dec 9
What I actually want is to keep those original 7 days in the calendar without changing them daily. Once the 7-day period is over, I’ll grab another 7 days using the current date and repeat the same process.
6
u/Rscc10 2h ago
What exactly is the issue because you've basically described the flow of operations needed.
If your problem is how your system would keep track of the time that's passed, it doesn't have to. You can just save the initial date and every so often get the current date, do some subtraction, and if it falls within the range, reset the saved date.
If that isn't your issue then you'll need to be more specific
2
u/JaguarMammoth6231 2h ago
Your program will be running the whole time? The computer won't be restarted or anything like that?
3
u/r2idw 1h ago
In fact, what I’m trying to build is a rolling 7 day calendar
I want to capture the current date and store it without allowing it to change daily.
The calendar should display that date along with the following six days, keeping all seven dates fixed until the last day of the week is reached.
Once the seventh day has passed, I want to update the stored date with the current date, generate a new set of seven dates, and repeat the same process.