Page 1 of 2 12 LastLast
Results 1 to 15 of 16
  1. #1
    Alprashant is offline Novice
    Windows Vista Access 2010 64bit
    Join Date
    Sep 2013
    Posts
    9

    Calculate time between two working dates

    The below function returns correct time difference between workdays. However, it is excluding Saturday as per the code.


    It is calculating 06:30 am to 22:00 pm time for weekdays but I also want it to calculate the time from 10:00 to 13:30 on a Saturday.

    I am trying to use the NetworkMinutes function to achieve this. However, there is a problem getting the time for Saturday.
    Hope someone can help with this.


    Code:
    Option Compare Database
    Option Explicit
    '---------------------------------------------------------------------------------------
    ' Procedure : NetWorkMinutes
    ' Author    : Rod
    ' Date      : 13/12/2012
    ' Purpose   : Returns the number of work minutes between two date-time argumnets.
    '
    '             A date-time is held in Access as a double precision number, the integer
    '             part is a relative day number and the decimal part is the time of day.
    '             To strip out a 'pure' date I use the Int (integer) function. To separate
    '             the time of date I use date-time - Int(date-time). I have used DateAdd in
    '             this procedure for clarity; I could have simply added an integer to a
    '             date-time value to add days.
    '
    '             Much of the procedure is given over to checking and adjusting the supplied
    '             dates.  After each adjustment it is important to check that the end date-time
    '             is still later than the start date-time.
    '
    '             I update the arguments in place.  As they are passed by reference, the calling
    '             procedure may interrogate them once control is passed back to see what the
    '             actual adjusted values are.
    '---------------------------------------------------------------------------------------
     
    Public Function NetWorkMinutes(rdteStart As Date, rdteEnd As Date) As Long
        Dim lngFirstDayMins As Long
        Dim lngLastDayMins As Long
        Dim dteWork As Date
        Dim dteEnd As Date
        Dim intFullWorkDays As Integer
        Dim dblStartTime As Double
        Dim dblEndTime As Double
        
        'Supplied end date-time earlier or equal start date-time, so exit.
        If rdteEnd <= rdteStart Then NetWorkMinutes = 0: GoTo Exit_Procedure
        
        'Check that time of day is sensible and adjust if necessary.
        
        dblStartTime = TimeValue("06:30:00")
        dblEndTime = TimeValue("22:00:00")
        Select Case rdteStart - Int(rdteStart)
            Case Is < dblStartTime  'Earlier than 06:30 am.
                rdteStart = Int(rdteStart) + dblStartTime
            Case Is >= dblEndTime   'Later than 22:00 pm.
                rdteStart = DateAdd("d", 1, Int(rdteStart)) + dblStartTime
        End Select
        Select Case rdteEnd - Int(rdteEnd)
            Case Is <= dblStartTime 'Equal or earlier than 8 am.
                rdteEnd = DateAdd("d", -1, Int(rdteEnd)) + dblEndTime
            Case Is > dblEndTime    'Later than 5 pm.
                rdteEnd = Int(rdteEnd) + dblEndTime
        End Select
        
        'Adjusted end date-time earlier or equal start date-time, so exit.
        If rdteEnd <= rdteStart Then NetWorkMinutes = 0: GoTo Exit_Procedure
        
        'If the start date is a Saturday or Sunday then reset it to 8 am the
        'following Monday. If it's a holiday reset to following day.
        Do
            Select Case DatePart("w", rdteStart, vbMonday)
                Case 6  'Saturday
                    rdteStart = DateAdd("d", 2, Int(rdteStart)) + dblStartTime
                Case 7  'Sunday
                    rdteStart = DateAdd("d", 1, Int(rdteStart)) + dblStartTime
                Case Else
                    If IsHoliday(rdteStart) Then
                        rdteStart = DateAdd("d", 1, Int(rdteStart)) + dblStartTime
                    Else
                        Exit Do     'Not weekend or holiday.
                    End If
            End Select
        Loop
        
        'If the end date is a Saturday or Sunday then reset it to 5 pm the
        'previous Friday.  If it's a holiday reset it to the previous day.
        
        Do
            Select Case DatePart("w", rdteEnd, vbMonday)
                Case 6  'Saturday
                    rdteEnd = DateAdd("d", -1, Int(rdteEnd)) + dblEndTime
                Case 7  'Sunday
                    rdteEnd = DateAdd("d", -2, Int(rdteEnd)) + dblEndTime
                Case Else
                    If IsHoliday(rdteEnd) Then
                        rdteEnd = DateAdd("d", -1, Int(rdteEnd)) + dblEndTime
                    Else
                        Exit Do     'Not weekend or holiday.
                    End If
            End Select
        Loop
        
        'Adjusted end date-time earlier or equal start date-time, so exit.
        If rdteEnd <= rdteStart Then NetWorkMinutes = 0: GoTo Exit_Procedure
        
        'Special case if adjusted date start equals adjusted date end.
        'Else calculate first and last day minutes.
        
        If Int(rdteStart) = Int(rdteEnd) Then
            NetWorkMinutes = DateDiff("n", rdteStart, rdteEnd)
            GoTo Exit_Procedure
        Else
            lngFirstDayMins = DateDiff("n", rdteStart - Int(rdteStart), dblEndTime)
            lngLastDayMins = DateDiff("n", dblStartTime, rdteEnd - Int(rdteEnd))
        End If
        
        'Set the start and end dates for full work days (i.e. exclude first
        'and last days) and iterate through period testing for holidays and weekends.
        
        dteWork = Int(DateAdd("d", 1, rdteStart))
        dteEnd = Int(DateAdd("d", -1, rdteEnd))
        intFullWorkDays = 0
        Do Until dteWork > dteEnd
            If Not IsHoliday(dteWork) Then
                Select Case DatePart("w", dteWork, vbMonday)
                Case 6, 7
                Case Else
                    intFullWorkDays = intFullWorkDays + 1
                End Select
            End If
            dteWork = DateAdd("d", 1, dteWork)
        Loop
        
        NetWorkMinutes = CLng(intFullWorkDays * 540) + (lngFirstDayMins + lngLastDayMins)
        
    Exit_Procedure:
        Exit Function
    End Function

  2. #2
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    What is the problem - error message, wrong results, nothing happens?

    Have you step debugged? Follow the code as it executes. Do variables populate? Find where code deviates from expected behavior and fix, debug, repeat. See link at bottom of my post for guidelines.
    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
    Alprashant is offline Novice
    Windows Vista Access 2010 64bit
    Join Date
    Sep 2013
    Posts
    9
    I want something to show as
    Saturday Start time as "10:00:00"
    Saturday End time as "13:30:00"

    As per current code it is excluding Saturday and Sunday.

  4. #4
    ItsMe's Avatar
    ItsMe is offline Sometimes Helpful
    Windows XP Access 2003
    Join Date
    Aug 2013
    Posts
    7,862
    If you take the time to read the notes in the code you pasted you will see that this function is designed to only count minutes during the workday.

  5. #5
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    If you want to include weekends then modify the Case structure code to not adjust the date when weekend is detected, and instead set the dblStartTime and dblEndTime values.
    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.

  6. #6
    alansidman's Avatar
    alansidman is offline VIP
    Windows 7 32bit Access 2010 32bit
    Join Date
    Apr 2010
    Location
    Steamboat Springs
    Posts
    2,529
    Cross Posted to: http://www.excelforum.com/access-tab...in-access.html

    Did you try the changes I suggested? Which is what June is suggesting here.

  7. #7
    Alprashant is offline Novice
    Windows Vista Access 2010 64bit
    Join Date
    Sep 2013
    Posts
    9
    Dear Alan,

    Dear Alan,

    I have only followed your instructions and not cross posted.

    Quote:
    I made some changes. They are my best guess and untested. I would urge you to go back to whomever designed the code if my changes are not effective and ask for assistance.
    Unquote.

    The changes suggested by you were not effective and hence I seeked help from the access forum from where I got the original code.

  8. #8
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    It is the same issue and therefore a cross-post. It's irrelevant where you originally found the code, this is now your code. Modify as suggested. Use a Case structure to set the dblStartTime and dblEndTime values. The code has examples of Case. Adapt it.
    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.

  9. #9
    Alprashant is offline Novice
    Windows Vista Access 2010 64bit
    Join Date
    Sep 2013
    Posts
    9
    Dear June,
    I'm sorry but I am a newbie and hence unable to amend the code as suggested.
    I request you to please amend the code for me.
    The problem is that even if I set as follows:
    Code:
        Dim dblStartTime As Double
        Dim dblEndTime As Double
        Dim dblSatStartTime As Double
        Dim dblSatEndTime As Double
        dblStartTime = TimeValue("06:30:00")
        dblEndTime = TimeValue("22:00:00")
        dblSatStartTime = TimeValue("10:00:00")
        dblSatEndTime = TimeValue("13:30:00")
    I am unable to adjust so that it counts the correct time for Case 6.
    I'm trying this for quite long and I'm getting no way out.
    Awaiting your kind response.

  10. #10
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Removed bad code. See later post.
    Last edited by June7; 10-09-2013 at 12:39 PM.
    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.

  11. #11
    Alprashant is offline Novice
    Windows Vista Access 2010 64bit
    Join Date
    Sep 2013
    Posts
    9
    Thanks for the code June,

    It is calculating correct times between Weekdays and also on a Saturday. Please find attached excel file which shows Errors faced.
    Attached Files Attached Files

  12. #12
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Sorry, it was late last night. This works much better.
    Code:
    Public Function NetWorkMinutes(rdteStart As Date, rdteEnd As Date) As Long
        Dim dteWork As Date
        Dim i As Integer
        Dim intDays As Integer
        Dim intMinutes As Integer
        Dim dteStart As Date
        Dim dteEnd As Date
        
        If rdteEnd > rdteStart Then
            
            'If start date is a holiday reset to following day.
            If IsHoliday(rdteStart) Then
                rdteStart = DateAdd("d", 1, Int(rdteStart))
            End If
            
            'If end date is a holiday reset it to the previous day.
            If IsHoliday(rdteEnd) Then
                rdteEnd = DateAdd("d", -1, Int(rdteEnd))
            End If
            
            'iterate through period testing for holidays
            intDays = DateDiff("d", rdteStart, rdteEnd) + 1
            intMinutes = 0
            dteWork = rdteStart
            For i = 1 To intDays
                If Not IsHoliday(dteWork) Then
                    Select Case DatePart("w", rdteStart, vbMonday)
                        Case 6, 7
                            dteStart = IIf(i = 1, rdteStart, CDate(Int(dteWork) + TimeValue("10:00:00")))
                            dteEnd = IIf(i = intDays, rdteEnd, CDate(Int(dteWork) + TimeValue("13:30:00")))
                        Case Else
                            dteStart = IIf(i = 1, rdteStart, CDate(Int(dteWork) + TimeValue("6:30:00")))
                            dteEnd = IIf(i = intDays, rdteEnd, CDate(Int(dteWork) + TimeValue("22:00:00")))
                    End Select
                    intMinutes = intMinutes + DateDiff("n", dteStart, dteEnd)
                End If
                dteWork = dteWork + 1
            Next
            NetWorkMinutes = intMinutes
    
        End If
    
    End Function
    Last edited by June7; 10-09-2013 at 12:49 PM.
    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.

  13. #13
    Alprashant is offline Novice
    Windows Vista Access 2010 64bit
    Join Date
    Sep 2013
    Posts
    9
    Dear June,

    Cant get the correct results yet.

    - Sundays should be ignored even if work started on and/or ended on a Sunday.
    - Saturday timing is still calculating as 06:30 to 22:00 i/o 10:00 to 13:30
    - If work starts on Sunday and ends on Monday, the time is starting at Monday 00:00. (This should start at Monday 06:30)

    Please help to fix the bugs. I have attached the DB file for your reference.
    Attached Files Attached Files

  14. #14
    June7's Avatar
    June7 is online now VIP
    Windows 7 64bit Access 2010 32bit
    Join Date
    May 2011
    Location
    The Great Land
    Posts
    52,929
    Changes in red:
    Code:
    Public Function NetWorkMinutes(rdteStart As Date, rdteEnd As Date) As Long
        Dim dteWork As Date
        Dim i As Integer
        Dim intDays As Integer
        Dim intDay As Integer
        Dim intMinutes As Integer
        Dim dteStart As Date
        Dim dteEnd As Date
            
        If rdteEnd > rdteStart Then
            
            'If start date is a holiday reset to following day.
            If IsHoliday(rdteStart) Then
                rdteStart = DateAdd("d", 1, Int(rdteStart))
            End If
            
            'If end date is a holiday reset it to the previous day.
            If IsHoliday(rdteEnd) Then
                rdteEnd = DateAdd("d", -1, Int(rdteEnd))
            End If
            
            'iterate through period testing for holidays
            intDays = DateDiff("d", rdteStart, rdteEnd) + 1
            intMinutes = 0
            dteWork = rdteStart
            For i = 1 To intDays
                intDay = DatePart("w", dteWork, vbMonday)
                If Not IsHoliday(dteWork) And intDay <> 7 Then
                    Select Case intDay
                        Case 6
                            dteStart = IIf(i = 1, rdteStart, CDate(Int(dteWork) + TimeValue("10:00:00")))
                            dteEnd = IIf(i = intDays, rdteEnd, CDate(Int(dteWork) + TimeValue("13:30:00")))
                        Case Else
                            dteStart = IIf(i = 1, rdteStart, CDate(Int(dteWork) + TimeValue("06:30:00")))
                            dteEnd = IIf(i = intDays, rdteEnd, CDate(Int(dteWork) + TimeValue("22:00:00")))
                    End Select
                    intMinutes = intMinutes + DateDiff("n", dteStart, dteEnd)
                End If
                dteWork = dteWork + 1
            Next
            NetWorkMinutes = intMinutes
    
        End If
    
    End Function
    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.

  15. #15
    Alprashant is offline Novice
    Windows Vista Access 2010 64bit
    Join Date
    Sep 2013
    Posts
    9
    Dear June,
    The results are still not matching.
    It's a kind request if you could test and verify the results in the db file provided earlier.

Page 1 of 2 12 LastLast
Please reply to this thread with any new information or opinions.

Similar Threads

  1. Replies: 5
    Last Post: 09-11-2013, 03:42 PM
  2. Replies: 42
    Last Post: 03-01-2013, 06:58 AM
  3. Calculate duration between two dates
    By Tommy1005 in forum Queries
    Replies: 3
    Last Post: 07-02-2012, 05:41 AM
  4. Replies: 8
    Last Post: 03-30-2012, 06:05 AM
  5. calculate no. of working days
    By JOSE LUIS in forum Access
    Replies: 1
    Last Post: 02-01-2010, 03:55 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