Skip to main content
Participant
February 8, 2011
Answered

Coundtown help

  • February 8, 2011
  • 2 replies
  • 370 views

Trying to wrap my head around the math for a countdown timer.  I have to separate the days, hours, minutes, and seconds... so really I'll have 4 countdowns on the same page.  Here is what I have so far, the date I'm counting down to is March 12, 2011, 12PM.  The daysLeft works, but I don't know what to do for the others.

//Calendar
toDay = new Date();
fcdDate = new Date(2011, 3, 12, 12);
cdDate = fcdDate-toDay;
daysLeft = Math.floor(cdDate/86400000);
hoursLeft = Math.floor(cdDate/?);
minsLeft = Math.floor(cdDate/?);
secsLeft = Math.floor(cdDate/?);

*** daysLeft, hoursLeft, etc refers to a value I’m linking with dynamic text representing each of those respectively. I’m not sure what the 86400000 is.  Also, the fcdDate has to be set with a month behind for some reason (year, month-1, date, hour). 

Thanks in advance

This topic has been closed for replies.
Correct answer Ned Murphy

If your countdown date is in March, then your daysLeft cannot be correct.  Months are numbered 0 thru 11.  Here's is a fiunctional bit of countdown code.  All you need is the test_txt textfield to have it work.

this.onEnterFrame = function() {
   
    var today:Date = new Date();
    var currentTime = today.getTime();
   
    var targetDate:Date = new Date(2011, 2, 12, 12);
    var targetTime = targetDate.getTime();
   
    var timeLeft = targetTime - currentTime;
   
   
    var sec = Math.floor(timeLeft/1000);
    var min = Math.floor(sec/60);
    var hrs = Math.floor(min/60);
    var days = Math.floor(hrs/24);


    sec = String(sec % 60);
    if (sec.length < 2) {
        sec = "0" + sec;
    }
    min = String(min % 60);
    if (min.length < 2) {
        min = "0" + min;
    }
    hrs = String(hrs % 24);
    if (hrs.length < 2) {
        hrs = "0" + hrs;
    }
    days = String(days);
   
    var counter:String = days + ":" + hrs + ":" + min + ":" + sec;
    test_txt.text = counter;
}

2 replies

Ned Murphy
Ned MurphyCorrect answer
Legend
February 9, 2011

If your countdown date is in March, then your daysLeft cannot be correct.  Months are numbered 0 thru 11.  Here's is a fiunctional bit of countdown code.  All you need is the test_txt textfield to have it work.

this.onEnterFrame = function() {
   
    var today:Date = new Date();
    var currentTime = today.getTime();
   
    var targetDate:Date = new Date(2011, 2, 12, 12);
    var targetTime = targetDate.getTime();
   
    var timeLeft = targetTime - currentTime;
   
   
    var sec = Math.floor(timeLeft/1000);
    var min = Math.floor(sec/60);
    var hrs = Math.floor(min/60);
    var days = Math.floor(hrs/24);


    sec = String(sec % 60);
    if (sec.length < 2) {
        sec = "0" + sec;
    }
    min = String(min % 60);
    if (min.length < 2) {
        min = "0" + min;
    }
    hrs = String(hrs % 24);
    if (hrs.length < 2) {
        hrs = "0" + hrs;
    }
    days = String(days);
   
    var counter:String = days + ":" + hrs + ":" + min + ":" + sec;
    test_txt.text = counter;
}

ekoraAuthor
Participant
February 8, 2011

Can you tell I'm dyslexic?  I meant "CounTDown"