We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

# Determine difference between 2 times

on
Medium Priority
150 Views
i am writing a time and attendance application.  i have some datetime fields that contain the timein and timeout.  I would like to calculate the hours worked between these two times and round the hours to the 1/4 hour.

for example..

my table:

id | name | date | timein1 | timeout1 | timein2 | timeout2

or with data ..

1 | frank | 03/26/2006 | 8:00 AM | 12:00 PM | 1:00 PM | 5:15 PM

how can i cfset a variable for the correct total hours worked that day.  (for this record, the result for total hours worked should be .. 8.25)
Comment
Watch Question

## View Solution Only

Commented:
<cfset TimeIn1 = CreateDateTime(year(Qry_date),Month(Qry_date),Day(Qry_date),Hour(Qry_timein1),Minute(Qry_timein1),0)>
<cfset TimeOut1 = CreateDateTime(year(Qry_date),Month(Qry_date),Day(Qry_date),Hour(Qry_timeout1),Minute(Qry_timeout1),0)>
<cfset TimeIn2 = CreateDateTime(year(Qry_date),Month(Qry_date),Day(Qry_date),Hour(Qry_timein2),Minute(Qry_timein2),0)>
<cfset TimeOut2 = CreateDateTime(year(Qry_date),Month(Qry_date),Day(Qry_date),Hour(Qry_timeout2),Minute(Qry_timeout2),0)>

<cfset TimeWorked1 = DateDiff("n",TimeIn1,TimeOut1)/60>
<cfset TimeWorked2 = DateDiff("n",TimeIn2,TimeOut2)/60>
<cfset TotalTimeWorked = TimeWorked1 + TimeWorked2>

Not the solution you were looking for? Getting a personalized solution is easy.

CERTIFIED EXPERT

Commented:
Something like

<cfset morning = DateDiff(n, form.timein1, form.timeout1)/60> <!--- = difference between timein and out in minutes /60 to give hours --->
<cfset afternoon = DateDiff(n, form.timein2, form.timeout2)/60>
<cfset totaltime = morning + afternoon>

you'll probably have to play with rounding but should be close to what you want

datediff @ livedocs
http://livedocs.macromedia.com/coldfusion/6.1/htmldocs/functi58.htm

Commented:
Even better would be to calculate the difference using datediff when you pull from the database
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile