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

132
Visualizações
JS date giving wrong month

I've been working with dates in node for a while now. I had an issue where the date was giving the wrong date after adding months. I ran some tests.

const d = new Date('2021-05-01');
console.log(d); // 2021-05-01T00:00:00.000Z
d.setMonth(d.getMonth() + 1);
console.log(d); // 2021-05-31T00:00:00.000Z

I was confused why this wasn't just giving me 2021-06-01 after running

console.log(d.getMonth())

I realized it was returning 3 which is not May

Then I ran

console.log(d.toDateString())

Which gave me Fri Apr 30 2021

So my question is why does it make new Date('2021-05-01') actually April 30th?

about 4 years ago · Santiago Trujillo
2 Respostas
Responde à pergunta

0

I have node.js v17.3.0 and cannot recreate your problem (unfortunately I can't comment yet).

So my suggestion would be to check your node installation or perhaps reinstall node. After running your code in node REPL, I get Tue Jun 01 2021 as result. I live in Germany, I don't know if that helps.

about 4 years ago · Santiago Trujillo Relatório

0

I ended up finding out that its because of timezones. I should of guess that, but if anyone else is in need of a fix you can simple add the time to the date to bring it to the actual date.

const date = new Date('2021-05-01 00:00:00');
about 4 years ago · Santiago Trujillo 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