So I am trying to take a time that is application wide and display it in the user's timezone. Here is what I have worked up and it is working. However I am sure there is a better way to do this. The starting timezone will always be Central. Central = 5. I am only concerned with the US timezones but if there is an easy way to implement it worldwide I am down with that too. The text label I am working with will display like 4:00PM that is why there is so much substringing.
function timeZones() {
var timezone = new Date().getTimezoneOffset();
timezone = timezone / 60;
//STARTING TIMEZONE WILL ALWAYS BE CENTRAL
var difference = 5 - timezone;
$(".time-zone").each(function () {
var a = $(this).text();
var hour = a.substring(0, a.indexOf(":") - 1);
hour = parseInt(a);
var yourTime;
//East Coast
if (difference == 1) {
hour = hour + difference;
if (hour == 12) {
yourTime = hour + a.substring(a.indexOf(":"), a.indexOf("A") - 1) + "PM";
}
else if (hour > 12) {
hour = hour - 12;
yourTime = hour + a.substring(a.indexOf(":"), a.indexOf("A") - 1) + "PM";
}
else {
yourTime = hour + a.substring(a.indexOf(":"));
}
$(this).text(yourTime);
}
//Mountain
if (difference == -1) {
hour = hour + difference;
if (hour == 0) {
hour = 12;
yourTime = hour + a.substring(a.indexOf(":"));
}
else if (hour < 0) {
hour = 12 + hour;
yourTime = hour + a.substring(a.indexOf(":"), a.indexOf("P") - 1) + "AM";
}
else {
yourTime = hour + a.substring(a.indexOf(":"));
}
$(this).text(yourTime);
}
//West Coast
if (difference == -2) {
hour = hour + difference;
if (hour == 0) {
hour = 12;
yourTime = hour + a.substring(a.indexOf(":"));
}
else if (hour < 0) {
hour = 12 + hour;
yourTime = hour + a.substring(a.indexOf(":"), a.indexOf("P") - 1) + "AM";
}
else {
yourTime = hour + a.substring(a.indexOf(":"));
}
$(this).text(yourTime);
}
//Alaska
if (difference == -3) {
hour = hour + difference;
if (hour == 0) {
hour = 12;
yourTime = hour + a.substring(a.indexOf(":"));
}
else if (hour < 0) {
hour = 12 + hour;
yourTime = hour + a.substring(a.indexOf(":"), a.indexOf("P") - 1) + "AM";
}
else {
yourTime = hour + a.substring(a.indexOf(":"));
}
$(this).text(yourTime);
}
});
}