Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

189
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda