Hi folks!
I consider myself somewhere between a beginner and intermediate where I can read and understand what’s going on in someone else’s code reasonably well, but I’m still rather clueless if asked to write my own completely from scratch.
That said, I can’t figure out why the following code, (which I have qualms admitting I found on the interwebz) for a timer I’m using for a widget displays the current date as 66 days from the end date which is just only over 30.
var endDate:Date = new Date(2010,8,3);
var countdownTimer:Timer = new Timer(1000);
countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);
countdownTimer.start();
function updateTime(e:TimerEvent):void{
var now:Date = new Date();
var timeLeft:Number = endDate.getTime() - now.getTime();
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);
seconds %= 60;
minutes %= 60;
hours %= 24;
var sec:String = seconds.toString();
var min:String = minutes.toString();
var hrs:String = hours.toString();
var d:String = days.toString();
if (sec.length < 2) {
sec = “0” + sec;
}
if (min.length < 2) {
min = “0” + min;
}
if (hrs.length < 2) {
hrs = “0” + hrs;
}
var time:String = d + “d.” + hrs + “h.” + min + “m.” + sec + “s”;
time_txt.text = time;
}
Can someone tell me where I’ve managed to go wrong?
Thanks in advance!