Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

123
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda