Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

116
Views
Dates stuck in month loops

The dates go to one month either side and then get stuck in loops. Starting in June, it will go fine to end of July, or start of May, but then loop back to the end/start of those months instead of going further. globalDate is a React state defined const [globalDate, setGlobalDate] = useState(new Date());

Code Snippet:

//decreasing
const newDate = new Date();
newDate.setDate(globalDate.getDate() - 1);
if (globalDate.getMonth() !== newDate.getMonth()) {
    newDate.setMonth(globalDate.getMonth());
}
if (globalDate.getDate() <= 1) {
    newDate.setMonth(globalDate.getMonth() - 1);
    newDate.setDate(daysInMonth[newDate.getMonth()]);
}
setGlobalDate(newDate);

//increasing
const newDate = new Date();
newDate.setDate(globalDate.getDate() + 1);
if (globalDate.getMonth() !== newDate.getMonth()) {
    newDate.setMonth(globalDate.getMonth());
}
if (globalDate.getDate() >= daysInMonth[globalDate.getMonth()]) {
    newDate.setMonth(globalDate.getMonth() + 1);
    newDate.setDate(1);
}
setGlobalDate(newDate);

Full page source : https://github.com/Westsi/thynkr/blob/master/frontend/web/js/src/Planner.js

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The problem in the first code block ("decreasing") occurs when newDate.setMonth() is executed when newDate has a date that is the last day of the month, and the previous month has fewer days. So for instance, it happens when newDate is 31 May at the moment this call to setMonth is made. That call will adjust the date to 31 April, but that date automatically translates to 1 May as April only has 30 days, and so you get stuck in the month of May.

To avoid this kind of problems, just start with globalDate immediately and subtract or add one day. That's all. The overflowing into a next/previous month is something that JavaScript already deals with automatically. So instead of trying to do this yourself (and run into trouble), let JavaScript do this for you:

Decreasing logic:

const newDate = new Date(globalDate); // starting point!
newDate.setDate(globalDate.getDate() - 1);  // month overflow happens automatically!
setGlobalDate(newDate); // That's it!

Increasing logic:

const newDate = new Date(globalDate); // starting point!
newDate.setDate(globalDate.getDate() + 1);  // month overflow happens automatically!
setGlobalDate(newDate); // That's it!
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!