When the US Daylight Saving ends, we switch our clocks back 1 hour and this makes the 1 AM hour happen twice on that day. However, JavaScript behaves strangely for timestamps in the second 1 AM hour (after clock switch) and keeps reverting them to the daylight time of 1 hour before. This can be seen when trying to use setMinutes and setHours methods.
This code sets the minutes of a date to its current minutes value, and behaves as expected (browser has America/Los_Angeles time zone):
let date = new Date("2021-07-07T09:40:00.000Z");
console.log(date.toLocaleString("en-us", {timeZoneName: "short"}));
// "7/7/2021, 2:40:00 AM PDT"
date.setMinutes(date.getMinutes());
console.log(date.toLocaleString("en-us", {timeZoneName: "short"}));
// "7/7/2021, 2:40:00 AM PDT"
But this code doesn't:
let date = new Date("2021-11-07T09:50:00.000Z"); // DST ended on Nov 7 at 2 AM
console.log(date.toLocaleString("en-us", {timeZoneName: "short"}));
// "11/7/2021, 1:50:00 AM PST"
date.setMinutes(date.getMinutes());
console.log(date.toLocaleString("en-us", {timeZoneName: "short"}));
// "11/7/2021, 1:50:00 AM PDT" <-- Wrong time zone
So the Date object can correctly print timestamps in the second (standard time) hour, but it interprets the date as still in daylight time when using setMinutes().
Is this expected behavior? It seems odd that date.setMinutes(date.getMinutes()) should alter the date.