Why does this out put yesterdays daydate?

const day = new Date("2022-10-27");
const day = new Date("2022-10-27");
4 Replies
13eck
13eck3y ago
Because timezones. If you don't give a timezone offset new Date() assumes midnight UTC. For example, I am currently in GMT-5 US Central time zone:
new Date("2022-10-27") //Wed Oct 26 2022 19:00:00 GMT-0500 (Central Daylight Time)
new Date("2022-10-27") //Wed Oct 26 2022 19:00:00 GMT-0500 (Central Daylight Time)
If you do the math and add the 5-hour offset the time is exactly midnight GMT
Errtu
ErrtuOP3y ago
Ah okay makes sense. Thank you. I noticed when I changed format to mm-dd-yyyy it gives different log. I figured out how to get the offset number, I just cant figure out how you actually add it to the date.
13eck
13eck3y ago
Here's the spec for the date string: https://tc39.es/ecma262/#sec-date-time-string-format Add the +HH:MM or -HH:MM to set the time zone offset (in place of the Z in the string)
Errtu
ErrtuOP3y ago
thank you
Want results from more Discord servers?
Add your server