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

156
Visualizações
HTML Date Object Year Selection Affects Rest of Date

When I have a date preloaded into my date picker and then manually change the year, it alters the rest of the date. My guess is that new Date() is having a hard time reading the input.

default string within the input (not an instance of Date):

 2021-09-09

date after conversion using new Date()

Thu Sep 09 2021 00:00:00 GMT-0400 (Eastern Daylight Time)

This is all correct so far

Here I manually enter the number 2 in the year portion of the html input string

0002-09-09

date

 Mon Feb 09 2009 00:00:00 GMT-0500 (Eastern Standard Time)

It changed from Sept 9th 2021 to Feb 9th 2009. Clearly the date object is having trouble figuring out the 0002 part of the date string but I am not sure why. What I expect the date to be is Sept 9th 0002 within the date picker (09/09/0002) so I can further add the digits I want to fill out the year.

In terms of my code I am simply passing in the new date string to the date object onChange. The replace is done to fix timezone issues with the day (the issue where the day will be one day off from the chosen date using the date-picker)

const date = new Date(string.replace(/-/g, '/').replace(/T.+/, ''))
about 4 years ago · Juan Pablo Isaza
1 Respostas
Responde à pergunta

0

Why not simply leave the variable string as it is?

Instead of using new Date("0002/09/09"), use new Date("0002-09-09") - it works perfectly fine and delivers the desired result. Thus your replacement-method on the string is absolutely obsolete.

In your current code, the year gets mistaken for the month, that's why February 9th 2002 gets returned.

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