Results 1 to 6 of 6
  1. #1
    foyasoul is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    5

    Question MS Access 2010: How to calculate the difference between two dates & times

    Hi, I have a team of staff that manage a help desk. The hours are from 8:00 am to 5:00 pm. I would like to measure the response time, meaning the amount of time that it takes from when a ticket is assigned to our workgroup to when the ticket is acknowledged.


    Here's the code I borrowed from another thread, but the calculations aren't accurate.

    For example

    OpenDate is 8/1/13 7:48 AM


    AcknowledgedByTechTime is 08/01/2013 9:33:50 AM

    The result is 1 hrs 45 min, but the correct result should be 1 hrs 33 min

    I do not want to count the time prior to 8:00 am or after 5:00 pm.




    Code:
    Public Function WorkingHrsMins(OpenDate As Date, AcknowledgedByTechTime As Date) As String
    '....................................................................
    ' Name:  WorkingHrs
    ' Inputs:   StartDate As Date & time
    '  EndDate As Date & time
    ' Returns: Integer - number of hours not inclusive of weekends
    ' Author: Arvin Meyer
    ' Date:  February 19, 1997
    ' Modified by Steve S (ssanfu)
    ' Date: April 11, 2012
    ' Comment: Accepts two dates and returns the number
    ' of work hours and minutes between 8am and 5pm
    ' Note that this function does not account for holidays.
    '....................................................................
       On Error GoTo Err_WorkingHrs
    
    
       Const cBeginingTime As Date = #8:00:00 AM#
       Const cEndingTime As Date = #5:00:00 PM#
    
    
       Dim intCountDays As Integer
       Dim intHours As Integer
       Dim intMinutes As Integer
       Dim intTotalMin As Integer
       Dim dtStart As Date
       Dim dtEnd As Date
       Dim dtTemp As Date
       Dim tmStart As Date
       Dim tmEnd As Date
    
    
       intMinutes = 0
       intHours = 0
       intCountDays = 0
       intTotalMin = 0
       WorkingHrsMins = 0
    
    
       'check end date > start date
       If AcknowledgedByTechTime < OpenDate Then
          dtTemp = OpenDate
          OpenDate = AcknowledgedByTechTime
          AcknowledgedByTechTime = dtTemp
       End If
    
    
       'get just the date portion
       dtStart = Int(OpenDate)
       dtEnd = Int(AcknowledgedByTechTime)
       'get just the time portion
       tmStart = OpenDate - dtStart
       tmEnd = AcknowledgedByTechTime - dtEnd
    
    
       If dtStart = dtEnd Then
          intTotalMin = DateDiff("n", tmStart, tmEnd)
       Else
          'skip the first day
          dtStart = dtStart + 1
    
    
          Do While dtStart < dtEnd
             'Make the above < and not <= to not count the EndDate
    
    
             Select Case Weekday(dtStart)
                Case Is = 1, 7
                   'do nothing
                Case Is = 2, 3, 4, 5, 6
                   intCountDays = intCountDays + 1
             End Select
             dtStart = dtStart + 1
          Loop
          intTotalMin = intCountDays * 720
    
    
          'first day minutes
          intTotalMin = IIf(intTotalMin + DateDiff("n", tmStart, cEndingTime) < 0, 0, intTotalMin + DateDiff("n", tmStart, cEndingTime))
          'Last day minutes
          intTotalMin = IIf(intTotalMin + DateDiff("n", cBeginingTime, tmEnd) < 0, 0, intTotalMin + DateDiff("n", cBeginingTime, tmEnd))
    
    
    
    
       End If
       intHours = intTotalMin \ 60   'hours
       intMinutes = intTotalMin Mod 60   'minutes
    
    
       'return value
       WorkingHrsMins = intHours & " hrs  " & intMinutes & " min"
    
    
    Exit_WorkingHrs:
       Exit Function
    
    
    Err_WorkingHrs:
       Select Case Err
    
    
          Case Else
             MsgBox Err.Description
             Resume Exit_WorkingHrs
       End Select
    
    
    End Function
    Click image for larger version. 

Name:	forum1.jpg 
Views:	27 
Size:	76.9 KB 
ID:	13730

  2. #2
    June7's Avatar
    June7 is offline VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    53,601
    So do you want to eliminate ticket initiated before or after those range ends or round up/down the time? Why would you not want to consider all the time spent on ticket?
    How to attach file: http://www.accessforums.net/showthread.php?t=70301 To provide db: copy, remove confidential data, run compact & repair, zip w/Windows Compression.

  3. #3
    foyasoul is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    5
    Hi, I want to exclude that segment of time. So, like in the example I provided, the ticket came in or was assigned to our workgroup at 7:48 am. The staff's shift doesn't begin until 8:00 am, so I want to exclude the 12 min from counting towards how long it took them to acknowledge the ticket, because they weren't working when the ticket was actually opened. I hope that makes sense.

  4. #4
    ssanfu is offline Master of Nothing
    Windows XP Access 2000
    Join Date
    Sep 2010
    Location
    Anchorage, Alaska, USA
    Posts
    9,664
    Add the lines in blue to your code:
    Code:
       'get just the date portion
       dtStart = Int(OpenDate)
       dtEnd = Int(AcknowledgedByTechTime)
       'get just the time portion
       tmStart = OpenDate - dtStart
       tmEnd = AcknowledgedByTechTime - dtEnd
    
    '   check the starting time- set the starting to 8:00 if before 8AM
       If tmStart < cBeginingTime Then
          tmStart = cBeginingTime
       End If
    
    '   check the ending time - set the ending time to 5PM if after 5pm
       If tmEnd > cEndingTime Then
          tmStart = cEndingTime
       End If

  5. #5
    cphelps is offline Novice
    Windows 7 64bit Access 2003
    Join Date
    Apr 2013
    Location
    Australia
    Posts
    6
    Then I would be doing another If statement to set dtTempt to be the OpenDate if it falls between 8am and 5pm, or if it falls outside of these hours then you need to set dtTemp to be 8am for the appropriate day (e.g if the OpenDate is after midnight and before 8am then set it for 8am for that day but if it is after 5pm but before midnight set it for 8am the next day).

  6. #6
    foyasoul is offline Novice
    Windows XP Access 2010 32bit
    Join Date
    Sep 2013
    Posts
    5
    ssanfu - Perfect!!! Thank you so much for helping me!!! I really did try to figure it out on my own, but I have little to no experience. Again, thanks for the help! I appreciate everyone's responses!

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 2
    Last Post: 05-19-2013, 06:03 AM
  2. SQL Query, find difference in two times.
    By Creaturemagic in forum Queries
    Replies: 7
    Last Post: 05-13-2013, 12:08 AM
  3. Difference between Dates/Times
    By dr4ke in forum Queries
    Replies: 3
    Last Post: 06-26-2012, 06:30 AM
  4. Replies: 3
    Last Post: 08-07-2010, 06:05 PM
  5. Replies: 0
    Last Post: 08-07-2008, 07:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums