I have a countdown timer that will be used for events that happen locally central time, mountain time, etc. However, if someone on eastern or pacific time (or any variation of the 4 timezones) looks at the countdown timer, I want to make sure that it shows the time relative to their timezone.
I thought i could read the user’s timezone offset (Date.getTimezoneOffset) and subtract from it the hardcoded timezone offset of the event (passed as a variable from XML depending on the event’s “primary” timezone).
Am I getting the math here wrong? The “net” timezone offset from the user - the event timezone offsets SHOULD add into the countdown time.
However, Date.getTimezoneOffset, regardless of my Mac or PC timezone setting/time, keeps showing an Eastern timezone offset of 5 hours. What’s going on?
The time is correct when I hardcode an event timezone offset to anything other than Eastern time. I think the problem is that it’s not getting a user timezone offset OTHER than -5 hours for Eastern Time.
Code for the timer is here (assume that the target event timezone has been passed in):
this.onEnterFrame = function() {
var today:Date = new Date();
var currentYear = today.getFullYear();
var currentTime = today.getTime();
var currentOffset = today.getTimezoneOffset() * 60 * 1000;
var targetDate:Date = new Date(countTimeYear,countTimeMonth,countTimeDate,countTimeHour,countTimeMinute);
var targetTime = targetDate.getTime();
var targetOffset:Number;
switch(countTimeZone) {
case "eastern" :
targetOffset = 5;
break;
case "central" :
targetOffset = 6;
break;
case "mountain" :
targetOffset = 7;
break;
case "pacific" :
targetOffset = 8;
break;
default :
targetOffset = 5;
break;
}
targetOffset = targetOffset * 60 * 60 * 1000;
var totalOffset = targetOffset - currentOffset;
var timeLeft = targetTime - currentTime + totalOffset;
var millisec = Math.floor(timeLeft);
var seconds = Math.floor(timeLeft/1000);
var minutes = Math.floor(seconds/60);
var hours = Math.floor(minutes/60);
var days = Math.floor(hours/24);
millisec = String(millisec % 10);
seconds = String(seconds % 60);
if (seconds.length < 2) {
seconds = "0" + seconds;
}
minutes = String(minutes % 60);
if (minutes.length < 2) {
minutes = "0" + minutes;
}
hours = String(hours % 24);
if (hours.length < 2) {
hours = "0" + hours;
}
days = String(days);
if (days.length == 1) {
days = "000" + days;
} else if (days.length == 2) {
days = "00" + days;
} else if (days.length == 3) {
days = "0" + days;
}
if (timeLeft > 0) {
countdownTime = days + ":" + hours + ":" + minutes + ":" + seconds + "." + millisec;
} else {
countdownTime = "0000:00:00:00.0";
}
this.countdown_text.text = countdownTime;
}
Thoughts? Suggestions?
IronChefMorimoto