Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

145
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!