Skip to content

Instantly share code, notes, and snippets.

@igroff
Last active September 4, 2015 15:39
Show Gist options
  • Save igroff/8985427 to your computer and use it in GitHub Desktop.
Save igroff/8985427 to your computer and use it in GitHub Desktop.
getTimezoneOffsetHoursAndMinutes -- Really, is this not already somewhere?
// name is still bad, have yet to figure what would be more correct
// sign change differences make re-using the TimezoneOffset verbage
// confusing
Date.prototype.getTimezoneOffsetHoursAndMinutes = function(){
// offset in minutes
var tzo = this.getTimezoneOffset();
// determine 'direction' from GMT we are
// if offset is positive, we're 'in the past'
var behindGMT = tzo > 0;
// work in positive
tzo = Math.abs(tzo);
// find our hour and minute 'parts'
var hours = Math.floor(tzo/60);
var minutes = tzo % 60;
// pad, really only ever have 1 0 padding max
// 2 digits
minutes = ("0"+minutes+"").slice(-2);
hours = ("0"+hours+"").slice(-2);
// add our indication as to what side of GMT
// we're on
if (behindGMT){ hours = "-" + hours; }
//done
return hours + ":" + minutes;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment