?
Solved

Coldfusion: TimeFormat to Float

Posted on 2007-10-19
8
Medium Priority
?
677 Views
Last Modified: 2013-12-24
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
0
Comment
Question by:tnoe0131
  • 4
  • 3
8 Comments
 
LVL 14

Expert Comment

by:Scott Bennett
ID: 20111888
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>
0
 
LVL 14

Expert Comment

by:Scott Bennett
ID: 20111980
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>
0
 
LVL 14

Accepted Solution

by:
Scott Bennett earned 2000 total points
ID: 20111991
looks like I made a copy and paste error in my last post. it shoud be:

<cfoutput>#datediff("n",rsGetAssignment.ASS_STARTTIME,FORM.CheckOutTime)/60#</cfoutput>
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
LVL 52

Expert Comment

by:_agx_
ID: 20112011
I would suggest using date functions to calculate the value.

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

Expert Comment

by:Scott Bennett
ID: 20112111
>> <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
0
 
LVL 52

Expert Comment

by:_agx_
ID: 20112221
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 >
0
 
LVL 52

Expert Comment

by:_agx_
ID: 20112258
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.   ;)
0
 

Author Comment

by:tnoe0131
ID: 20112688
_agx_ and SBennet - I appreciate both of your answers - and how fast you responded - it's working beautifully now!
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

What You Need to Know when Searching for a Webhost Provider
Windocks is an independent port of Docker's open source to Windows.   This article introduces the use of SQL Server in containers, with integrated support of SQL Server database cloning.
Via a live example, show how to set up a backup for SQL Server using a Maintenance Plan and how to schedule the job into SQL Server Agent.
Viewers will learn how to use the SELECT statement in SQL and will be exposed to the many uses the SELECT statement has.
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question