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
creating javascript Date object is not working properly

what is the problem and how to fix this ?

> new Date(2022,5,31)
< Fri Jul 01 2022 00:00:00 GMT+0430 (Iran Daylight Time)
> new Date("2022-5-31")
< Tue May 31 2022 00:00:00 GMT+0430 (Iran Daylight Time)

creating Date(2022,5,31) object

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

0

Month is 0 indexed, i.e. 0 represents January. When you pass in a string with explicit date formatting like "2022-5-31" the month is not 0 indexed.

> new Date(2022,5,31)
                ^
              June
                   ^ 
                   31 -> 01 of the next month (Jul)
< Fri Jul 01 2022 00:00:00 GMT+0430 (Iran Daylight Time)

> new Date("2022-5-31")
                 ^
                May
                    ^
                   31 valid day
< Tue May 31 2022 00:00:00 GMT+0430 (Iran Daylight Time)

This becomes explicit when you try putting days of 35, for example:

> console.log(new Date(2022, 05, 35));
< Tue Jul 05 2022 00:00:00 GMT+0100 (British Summer Time)
about 4 years ago · Juan Pablo Isaza Relatório

0

In the first example, January is 0 while in the second example, January is 1.

June only has 30 days so the extra 31st rolls over into July.

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