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

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

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

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 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