0

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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
JRCast
  • 1
  • 1
  • 1

0 Answers0