Question:
Action Script 3 - my countdown timer changes on different timezones?
?
2010-03-20 20:24:07 UTC
Hey all,
I had to make a countdown timer in ActionScript 3 and I used this code:


//Create your Date() object
var endDate:Date = new Date(2010,3,5);
//Create your Timer object
//The time being set with milliseconds(1000 milliseconds = 1 second)
var countdownTimer:Timer = new Timer(1000);
//Adding an event listener to the timer object
countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);
//Initializing timer object
countdownTimer.start();
//Calculate the time remaining as it is being updated
function updateTime(e:TimerEvent):void
{

//Current time
var now:Date = new Date();
var timeLeft:Number = endDate.getTime() - now.getTime();

//Converting the remaining time into seconds, minutes, hours, and days
var seconds:Number = Math.floor(timeLeft / 1000);
var minutes:Number = Math.floor(seconds / 60);
var hours:Number = Math.floor(minutes / 60);
var days:Number = Math.floor(hours / 24);

//Storing the remainder of this division problem
seconds %= 60;
minutes %= 60;
hours %= 24;

//Converting numerical values into strings so that
//we string all of these numbers together for the display
var sec:String = seconds.toString();
var min:String = minutes.toString();
var hrs:String = hours.toString();
var d:String = days.toString();

//Setting up a few restrictions for when the current time reaches a single digit
if (sec.length < 2) {
sec = "0" + sec;
}

if (min.length < 2) {
min = "0" + min;
}

if (hrs.length < 2) {
hrs = "0" + hrs;
}

//Stringing all of the numbers together for the display
var time:String = d + " DAYS " + hrs + " HOURS ";
//Setting the string to the display
time_txt.text = time;
}




It works fine, but it takes the local machine's time to calculate the time left and it changes in different timezones. How can I synchronize it with server's time for example?

Thanks in advance!
Four answers:
Ratchetr
2010-03-20 21:04:17 UTC
I'm not sure I know exactly what you are trying to do here, but I think AS3 is doing the only rational thing it can.



You are comparing current time with 2010-3-5. Well...fact of life...2010-3-5 happens at different (absolute) times around the world.



Do you want your timer to end at 2010-3-5 in England, San Fransico, or Australia? Those are very different times. What about daylight savings time?



UTC (aka GMT) is one way around this. Look at the functions in the AS3 date class with UTC in the name. You'll need to specify your target date in UTC as well.
_anonymous_
2010-03-20 20:38:38 UTC
firstly, flash programs do not lie on the server. they are actually downloaded to the computer in a file with extension swf (Shockwave Flash)



if you need it to sync with the server time, the program would have to ask for it somehow, maybe from a small script maintained on the server
Joseph
2010-03-20 21:58:32 UTC
It will calculate based on the time of the machine that you are using. Not much you can do. I like your code. It is really clean.
2016-04-12 05:34:13 UTC
I am flying to California for a week in the summer


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...