CFLib.org – Common Function Library Project

stringHours(mins)

Last updated November 26, 2010

author

Ryan Jeffords

Version: 1 | Requires: CF6 | Library: DateLib

Description:
This simple UDF will accept a number of minutes and return a string containing the format: "N hours N mins". This format can be slightly more optimal then just displaying "140 mins".

Return Values:
Returns a string.

Example:

<cfoutput>
<ul>
	<li>#stringHours("123")#</li>
	<li>#stringHours("568")#</li>
	<li>#stringHours("56")#</li>
	<li>#stringHours("3")#</li>
	<li>#stringHours("1357")#</li>
	<li>#stringHours("99")#</li>
	<li>#stringHours("45.26")#</li>
	<li>#stringHours("9871")#</li>
	<li>#stringHours("54")#</li>
	<li>#stringHours("10")#</li>
</ul>
</cfoutput>

Parameters:

Name Description Required
mins Numerical number of minutes. Yes

Full UDF Source:

<!---
 Formats a number of minutes into into a nicely formatted string.
 
 @param mins 	 Numerical number of minutes. (Required)
 @return Returns a string. 
 @author Ryan Jeffords (rjeffords@me.com) 
 @version 1, November 26, 2010 
--->
<cffunction name="stringHours" returntype="string" output="false">
    <cfargument name="mins" type="numeric" required="true" />
    
    <cfset var retVal = "" />
	<cfset var rawHours = mins/60 />
	
	<!--- Calculate the hours --->
	<cfset var hours = int(rawhours) />	
    <cfif hours is "1">
		<cfset hours = hours & " hour " />
	<cfelse>
		<cfset hours = hours & " hours " />
    </cfif>
    
    <!--- Calculate the minutes --->
    <cfset mins = round((rawHours - int(rawHours)) * 60) />	
    <cfif mins is "1">
		<cfset mins = mins & " min " />
    <cfelse>
		<cfset mins = mins & " mins " />    
    </cfif>
    
    <!--- Add the hours --->
    <cfif left(hours, 1) IS NOT 0>
		<cfset retVal = hours />
    </cfif>
    
    <!--- Now bring them all together --->
    <cfif left(mins, 1) IS NOT 0>
		<cfset retVal = retVal & mins />
	</cfif>
    
    <cfreturn retVal />
</cffunction>
blog comments powered by Disqus

Search CFLib.org


Latest Additions

Kevin Cotton added
date2ExcelDate
May 5, 2016

Raymond Camden added
CapFirst
April 25, 2016

Chris Wigginton added
loremIpsum
January 18, 2016

Gary Stanton added
calculateArrival...
November 19, 2015

Sebastiaan Naafs - van Dijk added
getDaysInQuarter
November 13, 2015

Created by Raymond Camden / Design by Justin Johnson