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

121
Views
Dates not incrementing correctly

I have a function that when called, it will add 7 days to the inputted date. The format for the date is YYYY-MM-DD (i.e. 2022-03-02)

for(let i = 0; i < repeatCount; i++)
{
    date = addWeek(date);
}

function addWeek(date)
{
    let temp = new Date(date);
    //console.log("Old Date: ");
    //console.log(temp.toISOString().split('T')[0])
    //console.log(temp.getDate());
    temp.setDate(temp.getDate() + 7);

    console.log("New Date: ");
    console.log(temp.toISOString().split('T')[0])

    //console.log("returning date");
    return temp.toISOString().split('T')[0];
}

For some reason, when the function is called as part of a repeat (React Web Application that involves recurring events), the addWeek function will not increment correctly a single time but then increment correctly the rest of the time.

Here's the input from my most recent log when I set repeatCount to 5:

Old Date:
2022-03-04
New Date:
2022-03-11

Old Date:
2022-03-11
New Date:
2022-03-17

Old Date:
2022-03-17
New Date:
2022-03-24

Old Date:
2022-03-24
New Date:
2022-03-31

Old Date:
2022-03-31
New Date:
2022-04-07

As you've probably noticed, it increments the week correctly with the exception of the second repeat. I've tested this multiple times with different dates, and each time, it is only the second iteration that is incremented incorrectly. Everything else works fine.

Please help. I'm losing my mind over this.

I forgot to add earlier: addWeek takes the date as a string input.

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

0

This is a Daylight Savings Time problem. Dates in JavaScript are inherently local, so when you use setDate() it tries to keep the same time of day on the new date, accounting for shifts in Daylight Savings Time. But that means the time of day will be different when compared to UTC time (which toISOString() converts to). The actual value of temp on the second output in your example is 2022-03-17T23:00Z, one hour before the date you were looking for. But your code strips off the time element so you end up one day off instead.

Instead of using setDate(), use the Date constructor:

    temp = new Date(temp.getFullYear(), temp.getMonth(), temp.getDate() + 7);

var date = new Date('2022-03-02');
const repeatCount = 5;
for(let i = 0; i < repeatCount; i++)
{
    date = addWeek(date);
}

function addWeek(date)
{
    let temp = new Date(date);
    temp = new Date(temp.getFullYear(), temp.getMonth(), temp.getDate() + 7);

    console.log("New Date: ", temp);
    console.log(temp.toISOString().split('T')[0])

    //console.log("returning date");
    return temp.toISOString().split('T')[0];
}

about 4 years ago · Juan Pablo Isaza Report

0

https://jsfiddle.net/4wm1vz9d/1/

function addWeek(date)
{
    date.setDate(date.getDate()+7)
}

let date = new Date();
console.log(date)
for(let i = 0; i < 5; i++)
{
    addWeek(date)
    console.log(date)
}
about 4 years ago · Juan Pablo Isaza Report

0

Phew. Javascript is sometimes weird. Seems like the date get's inaccurate because of the conversion to a ISO String. Try returning the temp Date object instead of the string. Then convert it after adding the weeks. It may have something to do with the other values which the Object provides...

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!