Home > AS3, Flash, Flex > Time conversion, Seconds to minutes, hours

Time conversion, Seconds to minutes, hours

Here is the function that will help you to convert the given seconds to time format.

public static function formatTime ( time:Number ):String
 {
 var remainder:Number;            
 var hours:Number = time / ( 60 * 60 );            
 remainder = hours - (Math.floor ( hours ));            
 hours = Math.floor ( hours );            
 var minutes:Number = remainder * 60;            
 remainder = minutes - (Math.floor ( minutes ));            
 minutes = Math.floor ( minutes );            
 var seconds:Number = remainder * 60;            
 remainder = seconds - (Math.floor ( seconds ));            
 seconds = Math.floor ( seconds );            
 var hString:String = hours < 10 ? "0" + hours : "" + hours;    
 var mString:String = minutes < 10 ? "0" + minutes : "" + minutes;
 var sString:String = seconds < 10 ? "0" + seconds : "" + seconds;                        
 if ( time < 0 || isNaN(time)) return "00:00";                        
 if ( hours > 0 )
 {            
 return hString + ":" + mString + ":" + sString;
 }else
 {
 return mString + ":" + sString;
 }
 }
Categories: AS3, Flash, Flex
  1. No comments yet.
  1. No trackbacks yet.

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 )

Connecting to %s

Follow

Get every new post delivered to your Inbox.