0
new Date('2020-10-25')
Sun Oct 25 2020 03:00:00 GMT+0300 (East Africa Time)

I want the hour to be 00:00:00 instead of 03:00:00. I don't want to pass the hour in the date, 2020-10-25 0:0.

Since the question is closed, I can't post my answer, hence I'm writing here,

new Date('10/25/2020') Using / instead of - working as expected.

Henok Tesfaye
  • 8,287
  • 13
  • 47
  • 84
  • The hour *is* 0. The default display just shows that datetime in your local timezone. Use `.toUTCString()` instead. – Bergi Jun 22 '20 at 07:37
  • 1
    "I want the hour to be 0, but I don't want to tell the code that the hour should be 0..." – Niet the Dark Absol Jun 22 '20 at 07:38
  • Pure date strings without a time portion passed to `Date` are treated as UTC and then adjusted based on the local timezone. If you don't want this to happen the easiest way is to pass the time part without adding a timezone at the end. Then the string would be interpreted as local time. I'm not sure why you are against this solution, though. – VLAZ Jun 22 '20 at 07:44

1 Answers1

0

you can set Hours to 0

var a = new Date();
a.setHours(0, 0, 0, 0);
iamhuynq
  • 5,357
  • 1
  • 13
  • 36
  • This will fail if you are in the western hemisphere. If you are in a -7 timezone, for example, `new Date("2020-06-22")` will produce `2020-06-21T17:00:00` (local). Setting the hour to zero will set it to midnight on *the previous day*. – VLAZ Jun 22 '20 at 08:45