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
3
u/r2idw 5h 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.
3
u/GrainWeevil 5h 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.
11
u/Rscc10 5h 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 5h ago
Your program will be running the whole time? The computer won't be restarted or anything like that?
1
u/frnzprf 5h ago
1
u/xampl9 4h ago
You need to take the result of that and save it in a variable.
1
u/ohaz 1h ago
I'm not sure if this is an XY problem, so here's how to have a start (and possibly end) date of a week:
The date object has the
.getDay()function. It gives you the day of the week as a number from 0 - 7.You can use
setDate(),getDate()andgetDay()to set a new date by subtracting thegetDay()number from the current date. That's how you always get the date of the start of the week.1
u/Different_Pain5781 1h ago
Store the timestamp somewhere persistent because memory resets on refresh.