Coldfusion: TimeFormat to Float


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.

Who is Participating?
Scott BennettManager TechnologyCommented:
looks like I made a copy and paste error in my last post. it shoud be:

Scott BennettManager TechnologyCommented:
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)>
Scott BennettManager TechnologyCommented:
as a short cut you can skip all those lines of code and just to this:
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

I would suggest using date functions to calculate the value.

<cfset floatHours = dateDiff("n", myStartTime, myEndTime) / 60 >
Scott BennettManager TechnologyCommented:
>> <cfset floatHours = dateDiff("n", myStartTime, myEndTime) / 60 >

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

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 >

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.   ;)
tnoe0131Author Commented:
_agx_ and SBennet - I appreciate both of your answers - and how fast you responded - it's working beautifully now!
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.