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

135
Visualizações
javascript getMonth() off by 2?

I understand that getMonth() is zero indexed so, for example, June is 5. But I'm getting that it's 'off' by 2 in some situations.

myDate = new Date(1622513190000)
month = myDate.getMonth()

console.log(myDate)
console.log(month)

Returns:

2021-06-01T02:06:30.000Z

4

Anyone have an idea why this is returning 4 and not 5? I'm using node v14.15.5 and am getting the expected result for other dates (September is 8, August is 7)

Thanks!

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

0

Hi and welcome here :)

Working with dates and timezone is a bit tricky, everyone here can attest it. For general information, I suggest you to read this article about Coordinated universal time.

The Z you can see in myDate value means Zulu time, military name for UTC±00:00, the reference timezone. ±00:00 represents an eventual offset with the Zulu time.

After having gone crazy about this question, I advice you to :

  • always get and store dates in Zulu format, and then compute time lapse with them
  • you can show users local dates, but it's only to display them. If you have to compute with a user input, first get the UTC equivalent (as @RobG advised).

Good luck ! :)

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