Flash AS3 Countdown to Midnight

A few days ago I needed to create a countdown timer that went to midnight of the current day.

I’m just posting the code here for reference later down the line if I need it. If you find the code useful then leave a comment 😉

On a side note this code could be used to countdown to any date / time (example below).


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
package  {
   
    import flash.display.MovieClip;
   
    import flash.utils.Timer;                           // Import the timer and the event for it
    import flash.events.TimerEvent;
   
    public class main extends MovieClip {
       
        // Digital 7 Font - by Chess-7
        // http://www.dafont.com/chess-7-chess-7.d1833
       
        var currentTime:Date;                           // We dont need to instantiate this yet as it will get the time it was created
        var endDate:Date = new Date();                  // Set the endDate to the time now
       
        var clockTimer = new Timer(1000);               // Make a new timer and set it to 1000 milliseconds (or 1 sec)
       
        public function main() {
            // ValidateThis.co.uk
            // Learn what getTime, setDate and setHours does here : http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001580.html
           
            // Figure out midnight today
            endDate.setDate(endDate.getDate() + 1);     // Set the date to tomorrow
            endDate.setHours(0,0,0,0);
           
            // Lets start the countdown
            clockTimer.addEventListener(TimerEvent.TIMER, timerTick);
            clockTimer.start();
           
            // Update the timer for the first time
            updateCountdown();
        }
       
        private function timerTick(e:TimerEvent):void {
            updateCountdown();
        }
       
        public function updateCountdown():void {
            var currentTime:Date = new Date();              // We now create a new Date and it will give us the current time
           
            // Get the current time difference
            var timeDifference:Number = endDate.getTime() - currentTime.getTime();          // Divide by 1000 because the results are in milliseconds
           
            // Calculate times
            var seconds:Number = Math.floor(timeDifference / 1000);
            var minutes:Number = Math.floor(seconds / 60);
            var hours:Number = Math.floor(minutes / 60);
            var days:Number = Math.floor(hours / 24);
           
            // Set the remainder of the division vars above
            hours %= 24;
            minutes %= 60;
            seconds %= 60;
           
           
            // If everything has counted down to 0
            if (seconds < 0 || minutes < 0 || hours < 0){
                myCountdown.txtTimeLeft.text = "Time over";
                clockTimer.stop();                      // Stop the clock from keep calling updateCountdown
            }else{
                // Create the strings for the clock so that we have a 0 infront of any number less than 10
                var tHours:String = (hours < 10) ? "0" + hours.toString() : hours.toString();
                var tMinutes:String = (minutes < 10) ? "0" + minutes.toString() : minutes.toString();
                var tSeconds:String = (seconds < 10) ? "0" + seconds.toString() : seconds.toString();
               
                // Set the text so we can read it
                myCountdown.txtTimeLeft.text = tHours + ":" + tMinutes + ":" + tSeconds;
            }
        }
    }
}
You can download a working copy here if you like
MidnightCountdown