Created
January 7, 2014 00:35
-
-
Save Danack/8292673 to your computer and use it in GitHub Desktop.
Timezone crap.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function getOffsetTime(timezoneString){ | |
var start = timezoneString.indexOf("("); | |
var end = timezoneString.indexOf(")"); | |
var length = end - start - 1; | |
var offsetString = timezoneString.substr(start + 1, length); | |
return offsetString; | |
} | |
//This was reading a list of timezone from a form. | |
//You'd either need to send a list of timezones, or figure out a way to | |
//get JS to list them. | |
function setupSelectedTimeZone(objectString){ | |
try{ | |
var bestMatch = 0; | |
var bestMatchValue = 100000; | |
var dateObject = new Date(); | |
//getTimezoneoffset returns negative value for time ahead of GMT | |
timezoneOffset = dateObject.getTimezoneOffset() * -60; | |
var selectObject = document.getElementById(objectString); | |
for (i=0; i <= selectObject.options.length - 1; i++){ | |
offsetTime = getOffsetTime(selectObject.options[i].value); | |
currentValue = Math.abs(timezoneOffset - offsetTime); | |
if(currentValue < bestMatchValue){ | |
bestMatch = i; | |
bestMatchValue = currentValue; | |
} | |
} | |
selectObject.selectedIndex = bestMatch; | |
} | |
catch(e){ | |
//alert("Exception setting setupSelectedTimeZone " + e); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment