Skip to content

Instantly share code, notes, and snippets.

@ijlyttle
Created July 31, 2018 20:56
Show Gist options
  • Save ijlyttle/1c7dcb9392c64bf79b46c53842bbda6b to your computer and use it in GitHub Desktop.
Save ijlyttle/1c7dcb9392c64bf79b46c53842bbda6b to your computer and use it in GitHub Desktop.
// reproduce problem setting system to "America/Los_Angeles"
date = new Date(Date.UTC(2011, 10, 06, 09, 00)); // 2011-11-06T09:00:00.000Z
console.log(date.toISOString())
date.setMinutes(0, 0, 0); // Should be a no-op!
console.log(date.toISOString()); // 2011-11-06T08:00:00.000Z
console.log()
// first-pass at solution
date = new Date(Date.UTC(2011, 10, 06, 09, 00)); // 2011-11-06T09:00:00.000Z
console.log(date.toISOString())
date.setUTCMinutes(0, 0, 0); // Should be a no-op!
console.log(date.toISOString()); // 2011-11-06T09:00:00.000Z (!!!)
console.log()
// second-pass at solution to take-into-account "Newfoundland" (1/2 hour zone)
date = new Date(Date.UTC(2011, 10, 06, 09, 00)); // 2011-11-06T09:00:00.000Z
console.log(date.toISOString())
offset = date.getTimezoneOffset();
date.setUTCMinutes(offset % 60, 0, 0); // Should be a no-op!
console.log(date.toISOString()); // 2011-11-06T09:00:00.000Z (still works)
console.log()
// repeat, having set my system time to Newfoundland
date = new Date(Date.UTC(2011, 10, 06, 04, 30)); // 2011-11-06T04:30:00.000Z
console.log(date.toISOString())
date.setMinutes(0, 0, 0); // Should be a no-op!
console.log(date.toISOString()); // 2011-11-06T03:30:00.000Z (still has problem)
console.log()
// repeat, having set my system time to Newfoundland
date = new Date(Date.UTC(2011, 10, 06, 04, 30)); // 2011-11-06T04:30:00.000Z
console.log(date.toISOString())
offset = date.getTimezoneOffset();
date.setUTCMinutes(offset % 60, 0, 0); // Should be a no-op!
console.log(date.toISOString()); // 2011-11-06T04:30:00.000Z (still works)
console.log()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment