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

20 comments sorted by

View all comments

3

u/r2idw 6h 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.

5

u/buzzon 6h 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 6h 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.

3

u/ALargeRubberDuck 5h ago

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

4

u/Vizeroth1 5h ago

Does your 7-day period need to start on an arbitrary day or can it be the current week starting on the most recent Sunday/Monday (or today if it is Sunday/Monday? If your week always starts on Sunday or Monday, then you can call getDay to figure out what day of the week it is and build it out from there.

Otherwise, you read your timestamp from storage and pass that to the date constructor (someone else suggested initializing the storage to 1970-01-01, or you could pass 0 or null to the constructor, but not undefined). Then you compare that to the current date, update your storage when you need to build a new 7-day period, etc.

3

u/IAmADev_NoReallyIAm 5h ago

No, no no.... going about it the wrong way. First, it sounds like your'e building a weekly calendar,,, not a rolling.. A rolling calendar would start with the current date and display the next X days... changing each day, ie, it's rolling. This sounds more like a weekly calendar that's based on Y being the start of the week.

So what you need to do is grab the current date, check to see if it matches the start of week for you, if so, you're done. If not, figure out the offset, subtract that from the current date, and that gets you the start of the week. Boom, you have you new date.

Example: Start of week is Sunday. Today is Wednesday, not the start of the week. The offset is 3 days, so we subtract three day from the current date Dec 2, 2026 - 3 days = Nov 29, 2026 <- that becomes your new start date for the week, render the calendar from there.

If Monday is your start, then you adjust... the offset becomes 2 instead of 3... and the resulting date is Nov 30,2026.

1

u/balefrost 3h ago

Do you always want those 7 days to start on the same day of the week, say Sunday (i.e. show Sunday -> Saturday)?

If so, could you instead take the current date, calculate from it the most recent Sunday (or whichever day you need), and use that to anchor your calendar?

edit I should have kept reading, it looks like I'm not the first person with this suggestion.