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

280
Vistas
Change day in new Date() js without rollover month, year or time

Hello everyone. Can you help me with this kind of question? I want to encrease days in date string, so code is kind like this.

var date = new Date()
date.setDate(date.getDate() + 1)

The problem is that i want to change only days without month, years or time (h m s). So, for example if i have date 31 december 2021 and i encrease day by one i want to get 01 december 2021 and not 01 january 2022. Is it possible in JS?

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

You'll just have to manually check and adjust if needed. Here's an example function:

var date = new Date('12/31/2021');

incremenetLoopingDate(date);
console.log(date);

function incremenetLoopingDate(date){
  var mo = date.getMonth();
  date.setDate(date.getDate() + 1);
  if(date.getMonth() !== mo){
    date.setMonth(mo);
  }
  return date;
}

about 4 years ago · Juan Pablo Isaza Denunciar

0

Similar to the other answer, but simpler. Just check if the incremented date is 1 and if so, set to the previous month. Note that this modifies the passed date.

function incrementMonthLoop(date = new Date()) {
  date.setDate(date.getDate() + 1);
  if (date.getDate() == 1) date.setMonth(date.getMonth() - 1);
  return date;
}

console.log(
  incrementMonthLoop(new Date(2021, 11, 31)).toDateString()
);

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