Why does this out put yesterdays daydate?

const day = new Date("2022-10-27");
const day = new Date("2022-10-27");
4 Replies
13eck
13eck2y 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
Errtu2y 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
13eck2y 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
Errtu2y ago
thank you
Want results from more Discord servers?
Add your server
More Posts