Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 3 of 3

Thread: converting milliseconds to hours and minutes

  1. #1

    Fla Box converting milliseconds to hours and minutes

    I have 2 Date objects and count down from now to ready,
    and show it in the format of "00 HOURS and 00 MINS."
    Code:
    var now:Date = new Date();
    var ready:Date = new Date(2008,9,10,12,0,0);
    
    var dif:Number = (ready.valueOf() - now.valueOf());
    trace(dif);
    How can I convert difference to HOUR + MIN?

    Please help.

    P.S. I`m using A.S. 3.0

  2. #2
    Some math to help you out:

    1 min = 60 000 ms
    1 h = 60 min = 3 600 000 ms

    With that you can see that to get the amount of minutes you do (dif/60000) and to get hours (dif/3600000).

  3. #3
    Quote Originally Posted by ViktorHesselbom View Post
    Some math to help you out:

    1 min = 60 000 ms
    1 h = 60 min = 3 600 000 ms

    With that you can see that to get the amount of minutes you do (dif/60000) and to get hours (dif/3600000).
    And subtract the already gathered hours (in minutes) from the calculated hours so you don't go over 59
    Look into the modulus (%) operator, it yields remainders so you could just read in ms and convert it to all the larger values in turn, leaving the excess for the smaller value. This class does that and allows you to add a textField to display everything:
    Code:
    package {
        import flash.utils.Timer;
        import flash.events.TimerEvent;
        import flash.text.TextField;
        import flash.text.TextFormat;
        import flash.display.DisplayObjectContainer;
        public class Countdown {
            private var ms:Number   = 0;
            private var secs:Number = 0;
            private var mins:Number = 0;
            private var hrs:Number  = 0;
            private var dys:Number  = 0;
            private var diff:Number = 0;
            private var timer:Timer;
            private var accuracy:String = "seconds";
            private var to:Date;
            private var tf:TextField;
            private var defaultFormat:TextFormat = new TextFormat("Arial",12);
            public function Countdown(to:Date,accuracy:String = "seconds") {
                this.accuracy=accuracy;
                this.to = to;
                var time:Number=0;
                switch(accuracy) {
                    case "milliseconds":
                        time = 1;
                        break;
                    case "seconds":
                        time = 1000;
                        break;
                    case "minutes":
                        time = 60000;
                        break;
                    case "hours":
                        time = 36000;
                        break;
                    case "days":
                        time = 24*36000;
                        break;
                }
                var now:Date = new Date();
                diff = to.valueOf() - now.valueOf();
                timer = new Timer(time);
                timer.addEventListener(TimerEvent.TIMER,update);
                timer.start();
                update(null);
            }
            private function update(e:TimerEvent){
                diff   -= timer.delay;
                ms      = diff;
                secs    = (ms      - ( ms      %= 1000 )) / 1000;
                mins    = (secs    - ( secs    %= 60   )) / 60;
                hrs     = (mins    - ( mins    %= 60   )) / 60;
                dys     = (hrs     - ( hrs     %= 24   )) / 24;
                handleOutput();
            }
            public function addText
            (par:DisplayObjectContainer,format:TextFormat=null,x:Number=0,y:Number=0) {
                if(format==null) {format=defaultFormat;}
                tf = new TextField();
                tf.setTextFormat(format);
                tf.defaultTextFormat = format;
                tf.x=x;
                tf.y=y;
                par.addChild(tf);
            }
            public function handleOutput() {
                if(tf!==null) {
                    tf.text = dys+":"+hrs+":"+mins+":"+secs;
                }
            }
            public function get milliseconds(){return ms            ;}
            public function get seconds()            {return secs        ;}
            public function get minutes()            {return mins        ;}
            public function get hours()                {return hrs            ;}
            public function get days()                {return dys            ;}
            public function get endDate()     {return to      ;}
        }
    }
    And the flash file has nothing in it but this on frame 1 (Obviously this is if you want to play with the format, you don't really need all the format junk):
    Code:
    import com.thecodebot.utils.Countdown;
    var then:Date = new Date(2008,12,25); //Christmas
    var count:Countdown = new Countdown(then,"seconds");
    var format:TextFormat = new TextFormat();
    format.bold = true;
    format.size = 12;
    format.font = "Courier";
    count.addText(stage,format);
    The class' bare bones functionality is to take the milliseconds, take out all the while seconds, and from the seconds, take out all the whole minutes, and so on, till it reaches days. This way you convert milliseconds to days:hours:minutes:seconds:milliseconds.

    I didn't snazz it up to play with the accuracy in diplaying the textField, this is just what I came up with off the top of my head that I just tested a minute ago
    Last edited by theCodeBot; September 30th, 2008 at 04:24 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012