-2

If you execute this in console

new Date('2020-06-16')

you will get

Mon Jun 15 2020 20:00:00 GMT-0400 (Eastern Daylight Time)

Why is this?

shenkwen
  • 3,536
  • 5
  • 45
  • 85
  • 4
    Does this answer your question? [Is the Javascript date object always one day off?](https://stackoverflow.com/questions/7556591/is-the-javascript-date-object-always-one-day-off) – sebasaenz Jun 20 '20 at 12:54
  • FYI, I've just updated the accepted answer to that question to clarify the timezone thing, since `new Date('2020-06-16')` gives you the result you're getting (because it's parsed in UTC) but, perhaps surprisingly, `new Date('2020-06-16T00:00:00')` would not (because it's parsed in local time). – T.J. Crowder Jun 20 '20 at 13:00

1 Answers1

0

new Date() generates the unix timestamp for UTC (+0000 timezone), then the string display is giving you that timestamp in your local timezone. EST is a negative timezone, so UTC happens before midnight and it appears as if you are still in the day before.

new Date('2020-06-16')
Tue Jun 16 2020 01:00:00 GMT+0100 (British Summer Time)

You can add a timezone to your dates if you want to specify local midnight rather than global midnight.

new Date('2020-06-16:00:00:00-4000')
Wed Jun 17 2020 17:00:00 GMT+0100 (British Summer Time) {}
James McGuigan
  • 7,542
  • 4
  • 26
  • 29
  • 1
    This question is a duplicate (marked a few minutes before your answer), and the dupetarget has a good answer. There's no need for another answer posted here. – T.J. Crowder Jun 20 '20 at 12:59
  • Question was brand new when it popped up on my feed. Took more than a few minutes to answer. – James McGuigan Jun 21 '20 at 00:18