Determine difference between 2 times

Posted on 2006-03-29
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
    LVL 8

    Accepted Solution

    <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

    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

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

    Featured Post

    How to improve team productivity

    Quip adds documents, spreadsheets, and tasklists to your Slack experience
    - Elevate ideas to Quip docs
    - Share Quip docs in Slack
    - Get notified of changes to your docs
    - Available on iOS/Android/Desktop/Web
    - Online/Offline

    Join & Write a Comment

    A web service ( is a software related technology that facilitates machine-to-machine interaction over a network. This article helps beginners in creating and consuming a web service using the ColdFusion Ma…
    Introduction This article explores the design of a cache system that can improve the performance of a web site or web application.  The assumption is that the web site has many more “read” operations than “write” operations (this is commonly the ca…
    In this sixth video of the Xpdf series, we discuss and demonstrate the PDFtoPNG utility, which converts a multi-page PDF file to separate color, grayscale, or monochrome PNG files, creating one PNG file for each page in the PDF. It does this via a c…
    Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

    732 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

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now