So I am working on a rather larger project and I ran into a bug and I wanted to see what I could be doing wrong. I loop though a large amount of data and round off some dates to clean my output into 15 minute buckets.
CODE:
//how I've been doing things
let expectedBehvaior = new Date("2020-11-01T05:14:00.000Z");
console.log(expectedBehvaior.toISOString());
expectedBehvaior.setMinutes(0);
console.log(expectedBehvaior.toISOString());
//using set minutes creates an issue for only hours 6 UTC on Nov 1st 2020
let crazyDate = new Date("2020-11-01T06:14:00.000Z");
console.log(crazyDate.toISOString());
crazyDate.setMinutes(0);
console.log(crazyDate.toISOString());
//how i fixed it
let fixedDate = new Date("2020-11-01T06:14:00.000Z");
console.log(fixedDate.toISOString());
fixedDate.setTime(fixedDate.getTime() - 60000 * fixedDate.getMinutes());
console.log(fixedDate.toISOString());
OUTPUT:
2020-11-01T05:14:00.000Z
2020-11-01T05:00:00.000Z
2020-11-01T06:14:00.000Z
2020-11-01T05:00:00.000Z
2020-11-01T06:14:00.000Z
2020-11-01T06:00:00.000Z
This seems related to daylight savings time in the US as the same thing happens on November 3rd 2019. Not sure how Date.setMinutes works in the background and was hoping someone could explain the behavior.
Thank you.
EDIT:
I am expecting the hour to remain the same after I call crazyDate.setMinutes(0). Instead it sets it back to 0500 UTC as opposed to 0600 UTC.