Im using the following code to make a simple countdown time for a website:
//Create your Date() object
var endDate:Date = new Date(2009,8,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;
}
if (d.length < 2) {
d = "0" + d;
}
//Stringing all of the numbers together for the display
var time:String = d + ":" + hrs + ":" + min + ":" + sec;
//Setting the string to the display
time_txt.text = time;
}
Which works fine.
I want to be able to input the date to count down to from and xml file (so it can be changed without having to mess with the fla)
I have tried the following:
//load in xml
var xml:int = 0
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, loadXML);
loader.load(new URLRequest("countdown.xml"));
function loadXML(e:Event):void
{
var xml= new XML(e.target.data) ;
trace(xml);
}
//Create your Date() object
var endDate:Date = new Date(xml);
(rest of the code as above but omitted for example)
The trace statement shows that the right date is being loaded from the xml file but the countdown is out by a few thousand days!
I was thinking that it may have something to do with the data type being an int rather than a string, but not too sure. Any ideas how to make this work?
Edit:
Here is the XML format for the original question:
<countdown> 2009,8,5 </countdown>