How To Convert Seconds to Minutes in ActionScript 3

April 17, 2010

Recently, I’ve been developing a game in ActionScript 3 (which I’ll share later). I needed to have a second timer count down, which is easy enough with the Timer class. The hard part for me was formatting it so it made sense to the player. Here’s what I came up with for converting a second count to minutes (formatted string):

public function convertSecondsToMinutes(seconds:Number):String
{
     var secondsString:String = new String;

     if (this.time%60 < 10) // checks to see if it needs a leading zero
     {
          secondsString = "0" + this.time%60;
     }
     else
     {
           secondsString = "" + this.time%60;
           // the double quotes are needed to cast this as a string
     }
     var minutes:String=Math.floor(this.time/60) + ':' + secondsString;

     return minutes;
}

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive