Link to home
Start Free TrialLog in
Avatar of tnoe0131
tnoe0131

asked on

Coldfusion: TimeFormat to Float

Hi,

I have the following:

      <cfset myStartTime = TimeFormat(#rsGetAssignment.ASS_STARTTIME#,'hh:mm:ss tt')>
        <cfset myEndTime = TimeFormat(#FORM.CheckOutTime#,'hh:mm:ss tt')>
        <cfset myHours = TimeFormat(#myEndTime# - #myStartTime#,'hh:mm')>

myHours gives the the right answer in hh:mm format.  However, I need to convert this to a decimal to put into a Float field in my MS SQL database.

For example, if I get myHours to be 07:30, I need it to be 7.5.

Any suggestions?  I'm a newbie, so be gentle... ha ha.

Thanks,
Todd
Avatar of Scott Bennett
Scott Bennett
Flag of United States of America image

if it's always going to be in that format then you could do it like:

<cfset myHours = "07:30">
<cfset decimalhours = listgetat(myHours,1,":") + (listgetat(myHours,2,":")/60)>
<cfoutput>#decimalhours#</cfoutput>
as a short cut you can skip all those lines of code and just to this:
<cfoutput>#datediff("n",#rsGetAssignment.ASS_STARTTIME,FORM.CheckOutTime)/60#</cfoutput>
ASKER CERTIFIED SOLUTION
Avatar of Scott Bennett
Scott Bennett
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I would suggest using date functions to calculate the value.

<cfset floatHours = dateDiff("n", myStartTime, myEndTime) / 60 >
<cfoutput>#floatHours#</cfoutput>
>> <cfset floatHours = dateDiff("n", myStartTime, myEndTime) / 60 >

looks like the exact same solution I just posted 2 mins earlier:

datediff("n",rsGetAssignment.ASS_STARTTIME,FORM.CheckOutTime)/60
Oops.  I see some more comments were added.

> <cfset myEndTime = TimeFormat(#FORM.CheckOutTime#,'hh:mm:ss tt')>

TimeFormat() doesn't really do much here since it returns a string _not_ a datetime object.  In other words, you're just converting a string to another string.   If you're going to do any type of validation/conversion better to parse the values into datetime objects instead, then use the datetime objects in your function.

<cfset floatHours = dateDiff("n", myStartTimeObject, myEndTimeObject) / 60 >
SBennett,

All I saw when I was posting was a comment about using string functions.  I thought of a better solution and posted it.  Obviously you must have had a similar thought since you posted a different solution a few minutes later.  Welcome to the stateless nature of the web.   ;)
Avatar of tnoe0131
tnoe0131

ASKER

_agx_ and SBennet - I appreciate both of your answers - and how fast you responded - it's working beautifully now!