Skip to main content
Participant
June 24, 2013
Answered

Help me about AS3 code of 24 hour countdown timer.

  • June 24, 2013
  • 2 replies
  • 2482 views

Hi,

I'm a beginner at flash.

i  just wanna ask about making 24hours countdown clock.

what i want is to make it only 24hour countdown clock and when it reaches the time it will start again.(every day)

and The start time is 2 o'clok(2 pm) every single day.(for the pacific time)

and I found this code from (http://forums.adobe.com/message/4116774 )

It's similar with what i want but even I tried, I can't change for mind...

and I want using hundredths too.

so here is the code.

Please help me,Thank you.

-----------------------------------------------

var endDate:Date = new Date(new Date().getTime()+24*60*60*1000);

var countdownTimer:Timer = new Timer(1000);

countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);

countdownTimer.start();

function updateTime(e:TimerEvent):void

{

          var now:Date = new Date();

if(now.getTime()>endDate.getTime()){

     time_txt.text = "00:00:00";

countdownTimer.stop();

return

}

          var timeLeft:Number = endDate.getTime() - now.getTime();

         

          var hundredth:Number = Math.floor(timeLeft / 10);

          var seconds:Number = Math.floor(hundredth / 1000);

          var minutes:Number = Math.floor(seconds / 60);

          var hours:Number = Math.floor(minutes / 60);

          seconds %= 60;

          minutes %= 60;

          var fs:String = hundredth.toString();

         

          var sec:String = seconds.toString();

          var min:String = minutes.toString();

          var hrs:String = hours.toString();

          if (fs.length < 2) {

                    sec = "0" + fs;

          }

          if (sec.length < 2) {

                    sec = "0" + sec;

          }

          if (min.length < 2) {

                    min = "0" + min;

          }

          if (hrs.length < 2) {

                    hrs = "0" + hrs;

          }

          var time:String = hrs + ":" + min + ":" + sec;

          time_txt.text = time;

}

This topic has been closed for replies.
Correct answer kglad

if you want endDate to be today at 2pm, among other ways to do this, you can use:

var endDate:Date = new Date();

endDate.setHours(14);

endDate.setMinutes(0);

endDate.setSeconds(0);

2 replies

Inspiring
June 26, 2013

Try code below - it is much less verbose, takes less calculations and I think meets your other requirements except for pacific time:

stop();

var endDate:Date;

var countdownTimer:Timer;

init();

function init():void

{

          initEndDate();

          countdownTimer = new Timer(50);

          countdownTimer.addEventListener(TimerEvent.TIMER, updateTime);

          countdownTimer.start();

}

function initEndDate():void

{

          endDate = new Date();

          endDate.setDate(endDate.getUTCDate() + 1);

          endDate.setHours(14);

          endDate.setMinutes(0);

          endDate.setSeconds(0);

          endDate.setMilliseconds(0);

}

function updateTime(e:TimerEvent):void

{

          var now:Date = new Date();

          now = new Date(endDate.time - now.time);

          if (now.time <= 0) {

                    initEndDate();

                    return;

          }

          time_txt.text = now.toUTCString().match(/(\d+:)+\d+/g) + ":" + String((now.millisecondsUTC < 10 ? "0" : "") + now.millisecondsUTC).match(/^\d\d/);

}

kglad
Community Expert
kgladCommunity ExpertCorrect answer
Community Expert
June 24, 2013

if you want endDate to be today at 2pm, among other ways to do this, you can use:

var endDate:Date = new Date();

endDate.setHours(14);

endDate.setMinutes(0);

endDate.setSeconds(0);

glay004Author
Participant
June 24, 2013

wow. Thank U so much......

and can I ask one more thing?

How can I insert hundredth?

I wanna do like this

start_time = getTimer();

countdown = 7200000;

onEnterFrame = function () {

    elapsed_time = getTimer()-start_time;

    _root.count_down.text = time_to_string(_root.countdown-elapsed_time);

};

function time_to_string(time_to_convert) {

    elapsed_hours = Math.floor(time_to_convert/3600000);

    remaining = time_to_convert-(elapsed_hours*3600000);

    elapsed_minutes = Math.floor(remaining/60000);

    remaining = remaining-(elapsed_minutes*60000);

    elapsed_seconds = Math.floor(remaining/1000);

    remaining = remaining-(elapsed_seconds*1000);

    elapsed_fs = Math.floor(remaining/10);

    if (elapsed_hours<10) {

        hours = "0"+elapsed_hours.toString();

    } else {

        hours = elapsed_hours.toString();

    }

    if (elapsed_minutes<10) {

        minutes = "0"+elapsed_minutes.toString();

    } else {

        minutes = elapsed_minutes.toString();

    }

    if (elapsed_seconds<10) {

        seconds = "0"+elapsed_seconds.toString();

    } else {

        seconds = elapsed_seconds.toString();

    }

    if (elapsed_fs<10) {

        hundredths = "0"+elapsed_fs.toString();

    } else {

        hundredths = elapsed_fs.toString();

    }

    return hours+":"+minutes+":"+seconds+":"+hundredths;

}

kglad
Community Expert
Community Expert
June 24, 2013

you can use:

function updateTime(e:TimerEvent):void{

        var now:Date = new Date();

        if(now.getTime()>endDate.getTime()){

            time_txt.text = "00:00:00";

            countdownTimer.stop();

            return

        }

        var timeLeft:Number = endDate.getTime() - now.getTime();

        var hrs:int = Math.floor(timeLeft/(1000*60*60));

        var min:int = (Math.floor(timeLeft/(1000*60) - hrs*60));

        var sec:int = Math.floor(timeLeft/1000-hrs*60*60-min*60)%60;

        var hun:int = Math.floor(timeLeft/10-hrs*100*60*60-min*100*60-sec*100);

          var time:String = padF(hrs.toString()) + ":" + padF(min.toString())+ ":" + padF(sec.toString())+":"+padF(hun.toString());

          time_txt.text = time;

}

function padF(s:String):String{

    while(s.length<2){

        s="0"+s;

    }

    return s

}