The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.

# Thread: converting milliseconds to hours and minutes

1. 52
posts
Registered User

## 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 dif:Number = (ready.valueOf() - now.valueOf());
trace(dif);```
How can I convert difference to HOUR + MIN?

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

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. Originally Posted by ViktorHesselbom

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.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();
}
(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;
}
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            ;}
}
}```
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";
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.