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

162
Views
Why converting string dates with yyyy-mm-dd get strange results, but string dates with yyyy/mm/dd get correct ones?

In short, I have the string date 2022-04-01 (this is the startDate.value), and I need to convert it to a Date.

I have this code:

const startDate0Hours = new Date(new Date(startDate.value).setHours(0, 0, 0, 0));
console.log('this is the date (string):', startDate.value);
console.log('this is without replace:', new Date(startDate.value));
console.log('this is with replace (/-/g, "\/"):', new Date((startDate.value).replace(/-/g,'\/')));
console.log('this is with setHours(0,0,0,0):', startDate0Hours)

Which prints this: [screenshot][1]

this is the date (string): 2022-04-01
this is without replace: Thu Mar 31 2022 21:00:00 GMT-0300 (Brasilia Standard Time)
this is with replace (/-/g, "/"): Fri Apr 01 2022 00:00:00 GMT-0300 (Brasilia Standard Time)
this is with setHours(0,0,0,0): Thu Mar 31 2022 00:00:00 GMT-0300 (Brasilia Standard Time)

Why does string date with '-' gets converted a day before, and 21h, but string date with '/' get correctly converted?

about 4 years ago · Santiago Trujillo
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!