This is not a blog. So sue me!


Crikey, things are looking up!

Thursday, November 15, 2007

ActionScript 2.0 function to convert milliseconds to HH:MM or MM:SS

These functions are basically the same, convert a Number in milliseconds to a displayable time string HH:MM or MM:SS:

public function convertMillisecondsToHHMM (millisecs:Number):String {
    var hour:Number;
    var min:Number;
    var hourStr:String;
    var minStr:String;

    var lenMin:Number = millisecs/1000/60;
   
    if (lenMin == undefined || lenMin <=0) {
      return "00:00";
    }
    else if(lenMin > (99*60)+59){ // greater than can be shown in 99:59
      return "99:59";
    }
    else {
      // work out how many hours and mins and concatenate them for the time
      hour = Math.floor(lenMin/60); //get the number of hours
      min = Math.round(lenMin%60); //get the remainder 0-59 mins
     
      // convert to strings
      hourStr = hour.toString();
      minStr = min.toString();
     
      // if minStr is 2 character, leave it alone, if it's 1 char, pad it, if it's more than 2 make it "99"
      if (hourStr.length == 1) {
        hourStr = "0" + hourStr;
      }
      else if (hourStr.length > 2) {
        hourStr = "99"; // this is impossible!
      }
      // if secStr is 2 digits, leave it alone, if it's 1, pad it, if it's more than 2 make it "99"
      if (minStr.length == 1) {
        minStr = "0" + minStr;
      }
      else if (minStr.length > 2) {
        minStr = "59"; // this is impossible!
      }
     
      return hourStr + ":" + minStr;
    }
  }
 
  public function convertMillisecondsToMMSS(millisecs:Number):String {
    var min:Number;
    var sec:Number;
    var minStr:String;
    var secStr:String;

    var lenSec:Number = millisecs/1000;
   
    if (lenSec == undefined || lenSec <=0) {
      return "00:00";
    }
    else if(lenSec > (99*60)+59){ // greater than can be shown in 99:59
      return "99:59";
    }
    else {
      // work out how many minutes and seconds and concatenate them for the time
      min = Math.floor(lenSec/60); //get the number of minutes
      sec = Math.round(lenSec%60); //get the remainder 0-59 secs
     
      // convert to strings
      minStr = min.toString();
      secStr = sec.toString();
     
      // if minStr is 2 character, leave it alone, if it's 1 char, pad it, if it's more than 2 make it "99"
      if (minStr.length == 1) {
        minStr = "0" + minStr;
      }
      else if (minStr.length > 2) {
        minStr = "99"; // this is impossible!
      }
      // if secStr is 2 digits, leave it alone, if it's 1, pad it, if it's more than 2 make it "99"
      if (secStr.length == 1) {
        secStr = "0" + secStr;
      }
      else if (secStr.length > 2) {
        secStr = "59"; // this is impossible!
      }
     
      return minStr + ":" + secStr;
    }
  }

1 comment:

Shecky said...

This was extremely helpful. The comments were very clearly written. Thank you for posting this code!

Contributors

St Lawrence Rowing

Test content from SLRC