Skip to content

Instantly share code, notes, and snippets.

@donaldallen
Created March 14, 2014 21:22
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save donaldallen/9557256 to your computer and use it in GitHub Desktop.
Save donaldallen/9557256 to your computer and use it in GitHub Desktop.
Handle Daylight Savings Time (DST) in Javascript.
var today = new Date();
Date.prototype.stdTimezoneOffset = function() {
var jan = new Date(this.getFullYear(), 0, 1);
var jul = new Date(this.getFullYear(), 6, 1);
return Math.max(jan.getTimezoneOffset(), jul.getTimezoneOffset());
}
Date.prototype.dst = function() {
return this.getTimezoneOffset() < this.stdTimezoneOffset();
}
var utc_offset = '-7';
if (today.dst()) {
utc_offset = '-6';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment