CFLib.org – Common Function Library Project

decimal2Time(decimal [, timeMask])

Last updated August 9, 2005

Version: 1 | Requires: ColdFusion 5 | Library: DateLib

 
Rated 1 time(s). Average Rating: 5.0

Description:
Takes a decimal value between 0.001 and 23.99 and converts it to a time format.

Return Values:
Returns a string.

Example:

view plain print about
<cfoutput>#decimal2Time(15.25)#</cfoutput>

Parameters:

Name Description Required
decimal A number between 0 and 23.99. Yes
timeMask Mask for formatting. Defaults to hh:mm tt. No

Full UDF Source:

view plain print about
<cfscript>
/**
 * Converts decimal to time.
 * 
 * @param decimal      A number between 0 and 23.99. (Required)
 * @param timeMask      Mask for formatting. Defaults to hh:mm tt. (Optional)
 * @return Returns a string. 
 * @author Nick Giovanni (audi.tt@verizon.net) 
 * @version 1, August 9, 2005 
 */

function decimal2Time(decimal){
    var timeMask = "hh:mm tt"
    var timeValue = ""
    var decimalMinutes = "";
    var decimalHours = "";

    //make sure passed value is numeric
    if(not isNumeric(decimal)) return "The value passed to function decimalToTime() is not a valid number!";

    timeValue =  numberFormat(decimal,"99.99");
    
    if(timeValue LT 0 OR timeValue GTE 24) return "The value passed to function decimalToTime() is not within the valid range of 0 - 23.99"

    //if the optional mask was passed use that otherwise default to "hh:mm tt"
    if(arrayLen(arguments) gt 1) timeMask = arguments[2];
            
    decimalHours = listfirst(timeValue,".");
    decimalMinutes = listLast(timeValue,".");
            
    //attempt to determine minutes 
    if(decimalMinutes neq 0) decimalMinutes = round(60*decimalMinutes/100);
            
    timeValue = timeFormat(decimalHours & ":" & decimalMinutes,timeMask);
    return timeValue;
}
</cfscript>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Dave Anderson Dave Anderson added
iniToStruct
20 day(s) ago

Dave Anderson Dave Anderson added
deDupeArray
20 day(s) ago

Richard Richard added
dice
22 day(s) ago

Isaac Dealey Isaac Dealey added
getRelative
a while ago

Top Rated

Darwan Leonardo Sitepu backupDatabase
Rated 5.0, 22 time(s)

Barney Boisvert indentXml
Rated 5.0, 10 time(s)

Kevin Pepperman generateSsccAsn
Rated 5.0, 4 time(s)

Raymond Camden highlightAndCrop
Rated 5.0, 4 time(s)

Created by Raymond Camden / Design by Justin Johnson