Schemaphic Systems Blog

Sharing experiences in Flash, Flex and AIR

Milliseconds formated to hours, seconds and minutes

with one comment

/*
@usage
var time:String = getTime(....., "-", false, true, true); // returns XX-XX as mins-secs
*/

public function getTime( value:int, separator:String=":", showHrs:Boolean=true, showMins:Boolean=true, showSecs:Boolean=false ) : String
{
    //milliseconds to hrs:mins:secs
    var hrs_str:String
    var mins_str:String;
    var secs_str:String;
    var time:String = "";

    var secs:Number;
    var mins:Number;
    var hrs:Number;

    if( value == 0 ) {
        secs = 0;
        mins =0;
        hrs = 0;
    } else {
        secs = Math.floor( value/1000 );
        mins = Math.floor(secs/60);
        hrs = Math.floor(mins/60);

        secs %= 60;
        mins %= 60;
    }
    if( hrs < 10 ){
        hrs_str = "0" + String( hrs );
    }else{
        hrs_str = String( hrs );
    }
    if( mins < 10 ){
        mins_str = "0" + String( mins );
    }else{
        mins_str = String( mins );
    }
    if( secs < 10 ){
        secs_str = "0" + String( secs );
    }else{
        secs_str = String( secs );
    }

    if( showHrs ){
        time += hrs_str + separator;
    }
    if( showMins ){
        time += mins_str + separator;
    }
    if( showSecs ){
       time += secs_str + separator;
    }
    time = time.substring( 0, time.length-1 );

    return time;
}
Advertisements

Written by Deepanjan Das

September 10, 2010 at 10:28 AM

One Response

Subscribe to comments with RSS.

  1. nice code snippit but I re-wrote the if else statements to single line code cause 58 lines is kidda long for 1 simple fubction.

    Err0rC0deX

    November 20, 2012 at 4:21 PM


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: