|
|
Joined: Dec 1999
Posts: 30,867
Master Elite Member
|
Time_Format() - Format seconds to a human-readable format
function Time_Format(MySeconds:uint, DisplayHours:int = 2, PadHours:Boolean = false, DisplaySeconds:Boolean = true, HourDelimiter:String = ':', MinuteDelimiter:String = ':'):String {
/*
Purpose
Used to convert a specified number of seconds into a standard display format: hh:mm:ss.
Parameters
MySeconds (uint) = The specified number of seconds we want to convert.
DisplayHours (optional int) = Determines when the hour-field is displayed.
Can be any of the following values:
0 Never Mode: The hour field is never displayed.
1 Always Mode: The hour field is always displayed.
2 Auto Mode: The hour field is only displayed if its value is > 0.
Default: 2
PadHours (optional Boolean) = Determines if the hours field is padded.
If true, the hours field always has two characters.
This means it will be prepended with a leading "0" (zero) if its value is less-than 10.
If false, the hours field will not be padded.
This means it will not be prepended with a leading "0" (zero) if it's value is less-than 10
If the hours field is not displayed, this value is moot (ignored).
Default: false
DisplaySeconds (optional Boolean) = Determines if seconds are displayed.
If true, the "seconds" field is displayed. If false, it is not displayed.
Default: true
HourDelimiter (optional String) = This is the string displayed between hours and minutes.
This may be a string of any length.
Default: ":" (colon)
MinuteDelimiter (optional String) = This is the string displayed between minutes and seconds.
This may be a string of any length.
Default: ":" (colon)
Return (String)
Returns the specified seconds (MySeconds) as a human-readable string in this format: hh:mm:ss
If hours are disabled (DisplayHours = 0), the minutes field may run above 60 so that the
correct time is always properly displayed.
Version History
1.0 2010.01.06 Tested by Russ Tanner on AS3. Please report bugs to russ@russtanner.com.
If you update this code, please post a copy of the latest version here:
http://orbisvitae.com/ubbthreads/ubbthreads.php?ubb=showflat&Number=55101&#Post55101
*/
// Calculate hours, minutes and seconds.
var hours:uint = (DisplayHours == 0) ? 0 : Math.floor(MySeconds / 3600); // If hours disabled, just set to zero.
var minutes:uint = (MySeconds - (hours * 3600)) / 60;
var seconds:uint = MySeconds - (hours * 3600) - (minutes * 60);
// Build return strings.
var sHours:String = ((DisplayHours == 0) || (DisplayHours == 2 && hours == 0)) ? '' : ((hours < 10 && PadHours) ? '0' + String(hours) : String(hours));
var sMinutes:String = (minutes < 10) ? '0' + String(minutes) : String(minutes);
var sSeconds:String = (DisplaySeconds) ? ((seconds < 10) ? '0' + String(seconds) : String(seconds)) : '';
// Build return string and return the result.
return ((sHours != '') ? sHours + HourDelimiter : '') + sMinutes + ((sSeconds != '') ? MinuteDelimiter + sSeconds : '');
}
|
|
|
|
Entire Thread
|
Time_Format() - Format seconds to a human-readable format
|
Russ
|
01/06/10 11:52 AM
|
|
|
|