Convert Milliseconds to Time (H:M:S)
Converting milliseconds to a time string can be a pain, especially when you're measuring something dynamic. In most languages I've come accross the Date object is calculated by the number of milliseconds that have passed since Jan 1, 1970. Because the different parts of a date are based on different bases ie: 60 minutes in an hour, 24 hours in a day etc. It's a lot easier to get the milliseconds passed since Jan1, 1970 and work with that value. This only works if you want to get the difference between two dates. If you were to subtract two dates(expressed as milliseconds) and get 432000000 milliseconds(5 days), then convert that to a Date object, the code would interpret 432000000 as Jan 6, 1970.
In another scenario I was just writing a podcast player in Flex 3/AIR and wanted to convert the Sound.length and SoundChannel.position values, both of which are measured in milliseconds, and display the length and current position of the episode in formats that would make sense. So I wrote a generic function that accepts millisecondsas an argument and returns the formatted time string.
/** Milliseconds to Time String in Flex 3 **/ /** Author: Yoav Givati [http://fightskillz.com] **/ public function fnMillisecondsToTimeCountUp(time:Number):String { //calculate playtime from milliseconds var h:Number = new Number(Math.floor(time/1000/60/60)); //minutes left shows total minutes left plus hours, 1h5m = 65mins //so we subtract the amount of 60's added by the hours to get just minutes var m:Number = new Number(Math.floor(time/1000/60)-(h*60)); //seconds left var s:Number = new Number(Math.floor(time/1000)-(m*60)); //create string variables var hours:String; var minutes:String; var seconds:String //make sure minutes and seconds are always two digits if(m.toString().length == 1) { minutes = "0"+m; } else { minutes = m.toString(); } if(s.toString().length == 1) { seconds = "0"+s; } else { seconds = s.toString(); } //if hours or minutes are 0 we don't need to see them if(h == 0) { hours = ''; if(m == 0) { minutes = ''; } else { minutes = minutes+":"; } } else { hours = h+":" minutes = minutes+":"; } // after 1 hour passes the seconds become 4 digits long // the last two of those digits represent the actual seconds seconds = seconds.slice(seconds.length-2, seconds.length); return hours+minutes+seconds; }
You'll notice that I'm using Math.floor()
, it's crucial that you round down, because the way the hours are being calculated for example, rounding up would show one hour had passed after only a fraction of an hour, just rounding up the minutes or seconds would cause everything to be out of sync and the math would be concussed. For those of you who are confused I should clarify that Math.floor(1.8)
would return a value of 1 and Math.ceil(1.3)
would return a value of 2, the term 'round' is probably a misleading. If you were using this function to count down instead of up, you would useMath.ceil()
(although still not for the hour value), you essentially want to stay on the 'other side' of the minute or second for as long as possible.
2 Comments | Dec 9, 2008