Skip navigation
Barry-Warren
Currently Being Moderated

Date countdown reset

Feb 28, 2012 7:55 AM

I've been using the below code for a countdown timer that ends on 5th April 2012. However when the countdown reaches this date I'll like it to automatically start counting down to 5th April 2013. Currently it goes into minus numbers. Thanks in advance for any help.

 

 

this.onEnterFrame = function() {

 

    var today:Date = new Date();

    var currentYear = today.getFullYear();

    var currentTime = today.getTime();

 

    var targetDate:Date = new Date(currentYear,3,5);

    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;

    time_txt.text = counter;

 

}

 
Replies
  • kglad
    72,227 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 28, 2012 8:27 AM   in reply to Barry-Warren

    var targetDate:Date = new Date(currentYear, 3, 5);

    var targetTime = targetDate.getTime();

     

    this.onEnterFrame = function() {

     

        var today:Date = new Date();

        var currentYear = today.getFullYear();

        var currentTime = today.getTime();

        var timeLeft = targetTime-currentTime;

        if(timeLeft<0){

            targetDate = new Date(currentYear+1,3,5);

            targetTime = targetDate.getTime();

            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(rs/24);

     

        time_txt.text = formatF(days)+"  "+formatF(hrs)+"  "+foramtF(min)+"  "+formatF(sec);

    };

     

    function formatF(n:Number):String{

        var s:String = n.toString();

        while(s.length<2){

            s="0"+s;

        }

        return s;

    }

     
    |
    Mark as:
  • kglad
    72,227 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 28, 2012 10:57 AM   in reply to kglad

    use:

     

     

     

     

    var targetDate:Date = new Date(currentYear, 3, 5);

    var targetTime = targetDate.getTime();

     

    this.onEnterFrame = function() {

     

        var today:Date = new Date();

        var currentYear = today.getFullYear();

        var currentTime = today.getTime();

        var timeLeft = targetTime-currentTime;

        if(timeLeft<0){

            targetDate = new Date(currentYear+1,3,5);

            targetTime = targetDate.getTime();

            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);

     

        time_txt.text = formatF(days)+"  "+formatF(hrs)+"  "+formatF(min)+"  "+formatF(sec);

    };

     

    function formatF(n:Number):String{

        var s:String = n.toString();

        while(s.length<2){

            s="0"+s;

        }

        return s;

    }

     
    |
    Mark as:
  • kglad
    72,227 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 29, 2012 6:55 AM   in reply to Barry-Warren

    currentYear wasn't defined in my last message.  this is a more efficient countdown timer that doesn't require repeated creation of new dates:

     

     

    var today:Date = new Date();

     

    var currentYear = today.getFullYear();

    var currentTime = today.getTime();

     

    var targetDate:Date = new Date(currentYear, 3, 5);

    var targetTime = targetDate.getTime();

     

    var startTime:Number = getTimer();

     

    this.onEnterFrame = function() {

     

        timeLeft = targetTime-currentTime-getTimer()+startTime;

       

        if (timeLeft<0) {

            targetDate = new Date(currentYear+1, 3, 5);

            targetTime = targetDate.getTime();

            timeLeft = targetTime-currentTime;

            startTime = getTimer();

        }

        timeLeft/=1000;

       

        var days:Number = Math.floor(timeLeft/(60*60*24));

        var hrs:Number = Math.floor((timeLeft-days*24*60*60)/(60*60));

        var min:Number = Math.floor((timeLeft-days*60*60*24-hrs*60*60)/60);

        var sec = Math.floor(timeLeft-days*60*60*24-hrs*60*60-min*60);

     

        time_txt.text = formatF(days)+"  "+formatF(hrs)+"  "+formatF(min)+"  "+formatF(sec);

    };

    function formatF(n:Number):String {

        var s:String = n.toString();

        while (s.length<2) {

            s = "0"+s;

        }

        return s;

    }

     
    |
    Mark as:
  • kglad
    72,227 posts
    Jul 21, 2002
    Currently Being Moderated
    Feb 29, 2012 9:10 AM   in reply to Barry-Warren

    you're welcome.

     
    |
    Mark as:

More Like This

  • Retrieving data ...

Bookmarked By (0)

Answers + Points = Status

  • 10 points awarded for Correct Answers
  • 5 points awarded for Helpful Answers
  • 10,000+ points
  • 1,001-10,000 points
  • 501-1,000 points
  • 5-500 points