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

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.

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.

3

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 4h 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.

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.

2

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

3

u/dmazzoni 3h ago

Are you reloading the web page every day?

If so, every time you do that it restarts your program from scratch.

Your problem isn’t with Date(), it’s with saving stuff between runs.

Learn localStorage.

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')

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
  • Write 1970-01-01 in a file. (For example. Or just leave it empty.)
  • When the function is called, it checks if it's five days after the stored date (or the file was empty).
  • If yes, replace the date with the current date.
  • Return the now saved date.

1

u/xampl9 4h ago

You need to take the result of that and save it in a variable.

1

u/ohaz 2h 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() and getDay() to set a new date by subtracting the getDay() number from the current date. That's how you always get the date of the start of the week.

const weekDays = new Date().getDay();
var startOfWeek = new Date();
startOfWeek.setDate(startOfWeek.getDate() - weekDays);
console.log(startOfWeek);

1

u/Different_Pain5781 1h ago

Store the timestamp somewhere persistent because memory resets on refresh.

•

u/Madscurr 8m ago

Do you need to do it in vanilla JS or can you use npm packages? Calendars (and really anything date-related) have so many complexities around timezones (if you haven't really looked into it then you really don't know) that you're best off using a package that has already worked out all the edge cases and weirdness and provides utility functions, like for getting the start of the week from a given date (in your case the start of the week containing now).