Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
The same function written two ways.
//Traditional Way
public function getClockFormat1(milliseconds:Number):String
{
var seconds:Number = milliseconds / 1000;
var m:int = 0;
if (seconds > 0) {
while (seconds > 59) {
m++;
seconds -= 60;
}
return String((m < 10 ? "0" : "") + m + ":" + (seconds < 10 ? "0" : "") + seconds);
}else{
return "00:00";
}
}
//My way
public function getClockFormat2(milliseconds:Number):String
{
var seconds:Number = milliseconds / 1000;
var hh:Number = Math.floor(seconds / 3600);
var mm:Number = Math.floor((seconds % 3600) / 60);
var ss:Number = Math.floor(seconds % 3600 % 60);
var cf:String = String((hh < 10 ? "0" + hh : hh) + ":" + (mm < 10 ? "0" + mm : mm) + ":" + (ss < 10 ? "0" + ss : ss));
return cf;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment