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

187
Visualizações
Javascript Loop Next 10 Days

I have the following code which I want to return an incremental date for the next 10 days

const startingDay = new Date('2021-10-29');
const thisDay = new Date(startingDay);

for(var i=1; i<10; i++) {
    thisDay.setDate(startingDay.getDate() + i);
  console.log(thisDay);
}

which is great, but for some reason as soon as it hits Oct 31, it starts going up in months rather than days.

Why would that be?

Thanks

about 4 years ago · Juan Pablo Isaza
3 Respostas
Responde à pergunta

0

for(var i=1; i<10; i++) {
    thisDay.setDate(startingDay.getDate() + i);

Since you're looping from 1 to i < 10, you'll add more days as the loop progresses.


I want to return an incremental date for the next 10 days

So why update thisDay with the startingDay.getDate() + i if you can just use thisDay.getDate() + 1:

const startingDay = new Date('2021-10-29');
const thisDay = new Date(startingDay);

for(var i=1; i<10; i++) {
    thisDay.setDate(thisDay.getDate() + 1);
    console.log(thisDay);
}

"2021-10-30T00:00:00.000Z"
"2021-10-31T00:00:00.000Z"
"2021-11-01T01:00:00.000Z"
"2021-11-02T01:00:00.000Z"
"2021-11-03T01:00:00.000Z"
"2021-11-04T01:00:00.000Z"
"2021-11-05T01:00:00.000Z"
"2021-11-06T01:00:00.000Z"
"2021-11-07T01:00:00.000Z"
about 4 years ago · Juan Pablo Isaza Relatório

0

Altering your logic slightly will give you the desired behaviour, setDate() will not work the way you expect with multiple calls to the same date instance.

The reason is that, starting with November 01, at iteration #4, we'll be setting the date to 33 (29 + 4). The logic used by setDate will be to advance the date to the next month, December, since November has only 30 days, then advancing another 2 days to December 3. The same effect will happen for subsequent iterations.

By re-creating the thisDay variable on each iteration, the logic used by setDate will work as expected.

const startingDay = new Date('2021-10-29');

for(var i= 1; i < 10; i++) {
    let thisDay = new Date(startingDay);
    thisDay.setDate(startingDay.getDate() + i);
    console.log(thisDay);
}
.as-console-wrapper { max-height: 100% !important; top: 0; }

about 4 years ago · Juan Pablo Isaza Relatório

0

Read the docs for https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date/setDate .

It will automatically switch to the next or previous month if you provide a value that is out of range for the current month. This makes it easier to jump between months when adding days.

But the way you've coded your example, you always start from the day of the original date, but edit the second date:

i:1 | date = 2021-10-29 -> set day to 30th -> date = 2021-10-30

i:2 | date = 2021-10-30 -> set day to 31st -> date = 2021-10-31

i:3 | date = 2021-10-31 -> set day to 32nd -> date = 2021-11-01, since oct only has 31 days

i:4 | date = 2021-11-01 -> set day to 33rd -> date = 2021-12-03, since nov has 30 days, so adding 33 days to the first of nov, gives us 3rd of december

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