Skip to content

Instantly share code, notes, and snippets.

@cantin
Created September 6, 2013 04:27
Show Gist options
  • Save cantin/6459562 to your computer and use it in GitHub Desktop.
Save cantin/6459562 to your computer and use it in GitHub Desktop.
//Accept zoneStr such as '-0400', '-04:00', '0400', '+0400'
//Return such as 4, -4, -4.75, -4.5
function tranformTimeZone(zoneStr) {
var signSymbol, hours, minutes, zone;
if (/^\+|-/.test(zoneStr)) {
signSymbol = zoneStr[0];
zoneStr = zoneStr.slice(1);
} else {
signSymbol = '+';
}
if (/:/.test(zoneStr)) {
hours = zoneStr.split(':')[0];
minutes = zoneStr.split(':')[1];
} else {
hours = zoneStr.slice(0, 2);
minutes = zoneStr.slice(2,4);
}
zone = parseInt(hours) + (parseInt(minutes) / 60);
if (signSymbol == '-') { zone = -zone }
return zone;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment