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

69
Visualizações
Adding months to date in js

This is the stack answer I followed: https://stackoverflow.com/a/5645110/1270259

To create:

            let start = new Date(data.start_date);
            let end   = new Date(start.setMonth(start.getMonth() + 1));

            start = start.toISOString().split('T')[0]
            end   = end.toISOString().split('T')[0]

            console.log(start, end);
  • Expected: start: 2022-07-23 end: 2022-08-23
  • Actual: start: 2022-07-23 end: 2022-07-23

Is this not how you properly add a month to a date? The stack answer states it is correct - am I missing something

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

0

By calling start.setMonth you end up updating the month on both dates. (This is noted in one of the comments on the answer you followed, but not in the answer itself.)

Separate the statements to only affect the date you want changed:

let start = new Date();
let end = new Date(start);
end.setMonth(end.getMonth() + 1);

console.log(start, end)

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