7 Replies Latest reply: Feb 29, 2012 9:10 AM by kglad RSS

    Date countdown reset

    Barry-Warren

      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;

       

      }

        • 1. Re: Date countdown reset
          kglad MVP

          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;

          }

          • 2. Re: Date countdown reset
            Barry-Warren Community Member

            Thanks, but it returns "NaN  9630  undefined  34669563". The last set of numbers counting down.

            • 3. Re: Date countdown reset
              kglad MVP

              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;

              }

              • 4. Re: Date countdown reset
                Barry-Warren Community Member

                Thanks but now the counter reads "400  9611  576679  34600760". The last set of numbers counting down.

                 

                Although I'm guessing this is most likely a formatting problem but the fact that it's showing 400 first suggests it's displaying the countdown to next year's date before it's finished counting down to this years.

                • 5. Re: Date countdown reset
                  kglad MVP

                  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;

                  }

                  • 6. Re: Date countdown reset
                    Barry-Warren Community Member

                    Yep that works. Thanks. I really appreciate this.

                    • 7. Re: Date countdown reset
                      kglad MVP

                      you're welcome.