Determine difference between 2 times

Posted on 2006-03-29
Medium Priority
Last Modified: 2013-12-24
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)
Question by:grizum

Accepted Solution

pmascari earned 840 total points
ID: 16329229
<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>
LVL 36

Expert Comment

ID: 16329240
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
LVL 35

Expert Comment

ID: 16337000
Even better would be to calculate the difference using datediff when you pull from the database

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

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

When it comes to showing a 404 error page to your visitors, you do not want that generic page to show, and you especially do not want your hosting provider’s ad error page to show either. In this article, I will show you how to enable the custom 40…
When it comes to security, close monitoring is a must. According to WhiteHat Security annual report, a substantial number of all web applications are vulnerable always. Monitis offers a new product - fully-featured Website security monitoring and pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses

850 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