Code for Business Hours -Help Please (1 Viewer)

Lilly420

Registered User.
Local time
Yesterday, 21:14
Joined
Oct 4, 2013
Messages
126
Hello,

I am trying to calculate the business hours between two dates (that include the date and time) in a query.

I did research on line and found some code that others said worked, but I am not getting the same results and not sure why--it seems to work on some dates but not others (examples below). If you look at the 2nd one on the chart, Start 7/29/19 2:00PM and finished 7/30/19 9:00AM—is says 2.13 hours? I do not know code well, and usually get help here when I get stuck, so thank you for that and appreciate your time.

Lilly

Results:
QryPerfMetricsBusHoursCodeOnly
MPDateExtractStarted
MPDateExtractFinished
BusHours
BusHoursFormat
7/26/2019 12:24:00 PM
7/26/2019 12:43:00 PM
0.316666666666667
0.32
7/29/2019 2:00:00 PM
7/30/2019 9:00:00 AM
2.13333333333333
2.13
7/30/2019 9:00:00 AM
7/30/2019 1:30:00 PM
4.5
4.50
8/1/2019 8:00:00 AM
8/2/2019 9:00:00 AM
8.13333333333333
8.13
8/1/2019 9:00:00 AM
8/2/2019 1:00:00 PM
7.13333333333333
7.13
7/31/2019 8:10:00 AM
7/31/2019 9:04:00 AM
0.9
0.90


This is the code:

Option Compare Database
Public Function Networkhours(dteStart As Date, dteEnd As Date) As Single

Dim intGrossDays As Integer
Dim intGrossHours As Single
Dim dteCurrDate As Date
Dim i As Integer
Dim WorkDayStart As Date
Dim WorkDayend As Date
Dim nonWorkDays As Integer
Dim StartDayhours As Single
Dim EndDayhours As Single

Networkhours = 0
nonWorkDays = 0
'Calculate work day hours on 1st and last day

WorkDayStart = DateValue(dteEnd) + TimeValue("08:00am")
WorkDayend = DateValue(dteStart) + TimeValue("04:00pm")
StartDayhours = DateDiff("n", dteStart, WorkDayend)
EndDayhours = DateDiff("n", WorkDayStart, dteEnd)
'adjust for time entries outside of business hours

If StartDayhours < 0 Then
StartDayhours = 0
End If
If EndDayhours > 8 Then
EndDayhours = 8
End If

'Calculate total hours and days between start and end times

intGrossDays = DateDiff("d", (dteStart), (dteEnd))
intGrossHours = DateDiff("n", (dteStart), (dteEnd))

'count number of weekend days and holidays (from a table called "Holidays" that lists them)
For i = 0 To intGrossDays
dteCurrDate = dteStart + i
If Weekday(dteCurrDate, vbSaturday) < 3 Then
nonWorkDays = nonWorkDays + 1
Else
If Not IsNull(DLookup("[HolDate]", "Holidays", "[HolDate] = #" & Int(dteCurrDate) & "#")) Then
nonWorkDays = nonWorkDays + 1
End If
End If
Next i
'Calculate number of work hours

Select Case intGrossDays
Case 0
'start and end time on same day
Networkhours = intGrossHours
Case 1
'start and end time on consecutive days
Networkhours = Networkhours + StartDayhours
Networkhours = Networkhours + EndDayhours
Case Is > 1
'start and end time on non consecutive days
Networkhours = Networkhours - (nonWorkDays * 1)
Networkhours = (intGrossDays - 1 - nonWorkDays) * 8
Networkhours = Networkhours + StartDayhours
Networkhours = Networkhours + EndDayhours

End Select

End Function


This is the query:

SELECT tblMonarchPerformance.MPDateExtractStarted, tblMonarchPerformance.MPDateExtractFinished, NetWorkhours([MPDateExtractStarted],[MPDateExtractFinished])/60 AS BusHours, Format([BusHours],"Fixed") AS BusHoursFormat
FROM tblMonarchPerformance;
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:14
Joined
May 7, 2009
Messages
19,237
you may try this one:
Code:
Public Function CountWorkHours(dt1 As Variant, dt2 As Variant) As Double
    Const OneMinute As Double = 6.94444444444442E-04
    Dim dtTmp As Date
    Dim dbldt1 As Double, dbldt2 As Double
    If IsNull(dt1) Or IsNull(dt2) Then
        Exit Function
    End If
    If dt1 > dt2 Then
        dtTmp = dt1
        dt1 = dt2
        dt2 = dtTmp
    End If
    dbldt1 = CDbl(dt1)
    dbldt2 = CDbl(dt2)
    CountWorkDays = (dbldt2 - dbldt1) / OneMinute / 60
End Function
 

Lilly420

Registered User.
Local time
Yesterday, 21:14
Joined
Oct 4, 2013
Messages
126
Thank you but that did not seem to work either below are the results I got:

MPDateExtractStartedMPDateExtractFinishedBusHours7/26/19 12:24 PM7/26/19 12:43 PM0.0052777787/29/19 2:00 PM7/30/19 9:00 AM0.3166666677/30/19 9:00 AM7/30/19 1:30 PM0.0758/1/19 8:00 AM8/2/19 9:00 AM0.4166666678/1/19 9:00 AM8/2/19 1:00 PM0.4666666677/31/19 8:10 AM7/31/19 9:04 AM0.015

Not sure how to accomplish this.

Lilly
 

Lilly420

Registered User.
Local time
Yesterday, 21:14
Joined
Oct 4, 2013
Messages
126
Sorry, hard to read the above:

7/26/19 12:24 pm - 7/26/19 pm = 0.05277778
7/29/19 2:00 PM - 7/30/19 AM = 0.31666667
7/30/19 9AM - 7/30/19 1:30PM = 0.075
7/31/19 8:10AM - 7/31/19 9:04AM = 0.015

It doesn't seem to be in bus hours...or am I missing something?

Lilly
 

Lilly420

Registered User.
Local time
Yesterday, 21:14
Joined
Oct 4, 2013
Messages
126
Well, I did not remove the /60 out of the query...I just saw this and removed it and I am getting the hours but not business hours if it should go into the next day...just the total hours, is there anyway to include business hours in this? So sorry for the error, and thank you for helping me.

Lilly
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:14
Joined
May 7, 2009
Messages
19,237
your dates are Out of business hours.
what happens if they are out of business hours, disregard their remaining hours, becoz its not between bus hours?
what the function is doing is summing the hours the workers had worked from period1 to period2. nothing more.
youll have lots of strike/complain if you disregard hours beyond business hours.
do they get OT hours beyond business hours. it's a regular business practice.
 

Mark_

Longboard on the internet
Local time
Yesterday, 18:14
Joined
Sep 12, 2017
Messages
2,111
OK, two steps back.

What is your definition of "Business hours"? For your example of
Start 7/29/19 2:00PM and finished 7/30/19 9:00AM
Would this be 19 hours or are you looking for a differnet answer?

Note, because dates are stored as an integer for the "Days" and decimal for the "Hours", the 19 hours comes out as .7917 if you simply subtract one from the other.

If you are looking to calculate "Working hours" and have times, days of the week, or other period that are not included, we can help you work out how to do the calculations.
 

Lilly420

Registered User.
Local time
Yesterday, 21:14
Joined
Oct 4, 2013
Messages
126
Thank you. We do Monarch Extracts for customers and trying to figure out how long it takes to do each extract and keep the data for reporting purposes. So I may have started the extract on 7/29/19 at 3pm and did not finish until the next day 7/30/19 at 10AM based on a 8 hour work day. So I was hoping to calculate that time...does that make sense or is there a better way to do this?

Thank you all for the help.

Lilly
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:14
Joined
May 7, 2009
Messages
19,237
so, what are the starting and ending time of the business 8am-5pm (minus 1 hr for noon break?)
 

Lilly420

Registered User.
Local time
Yesterday, 21:14
Joined
Oct 4, 2013
Messages
126
yes, that s correct, Monday thru Friday, 8am to 5pm (8 working hours).

Thank you.
 

theDBguy

I’m here to help
Staff member
Local time
Yesterday, 18:14
Joined
Oct 29, 2018
Messages
21,469
Hi. Pardon me for jumping in... I haven't looked at your code, so I don't know if it's the same as this one. But if it's not, maybe you could give it a try. Cheers!
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:14
Joined
May 7, 2009
Messages
19,237
Code:
'---------------------------------------------------------------------------------------------------
'
' compute Work hours between business hours
'
' arnelgp
' for Lilly420
' 1-aug-2019
'
' Note:
'
' dt1 and dt2 must have a Timestamp, eg. 7/12/2019 8:23 am
'
' sWorkHrStart      = business start time (string)
' sWorkHrEnd        = business end time (string)
' sBrkHrStart       = lunch break starts (string)
' sBrkHrEnd         = lunch break ends (string)
'
' Usage:
'
' WorkHrs(#1/1/2019 9 am#, #1/2/2019 2 pm#, "8 am", "5 pm", "12 pm", "1 pm")
'
' On your Query:
'
' Select WorkHrs([DateFieldStart], [DateFieldEnd], "8 am", "5 pm", "12 pm", 1 pm") As TotalHrsWorked
'
' please report any bugs and on which business dates does it fails.
'------------------------------------------------------------------------------------------------------
Public Function WorkHrs( _
                            dt1 As Date, _
                            dt2 As Date, _
                            Optional sWorkHrStart As String = "8 am", _
                            Optional sWorkHrEnd As String = "5 pm", _
                            Optional sBrkHrStart As String = "12 pm", _
                            Optional sBrkHrEnd As String = "1 pm") As Double
    Const hr1 As Double = 4.16666666715173E-02
    Const Min1 As Double = 6.94444444444442E-04
    
    Dim dStart As Date, dEnd As Date
    Dim minTotal As Double
    Dim tmpDate1 As Date, tmpDate2 As Date
    Dim dt As Date
    
    sWorkHrStart = " " & Trim(sWorkHrStart)
    sWorkHrEnd = " " & Trim(sWorkHrEnd)
    sBrkHrStart = " " & Trim(sBrkHrStart)
    sBrkHrEnd = " " & Trim(sBrkHrEnd)
    
    If dt1 > dt2 Then
        tmpDate1 = dt1
        dt1 = dt2
        dt2 = tmpDate1
    End If
    
    minTotal = 0#
    
    Select Case True
    Case DateValue(dt1) = DateValue(dt2)
    
        If InStr(1, "Saturday,Sunday", Format(dt1, "mmmm")) = 0 Then
            tmpDate1 = maxAmong(dt1, CDate(DateValue(dt1) & sWorkHrStart))
            tmpDate2 = minAmong(dt2, CDate(DateValue(dt2) & sWorkHrEnd))
            
            If CDbl(tmpDate2) - CDbl(tmpDate1) < (4 * hr1) Then
                minTotal = CDec(tmpDate2) - CDec(tmpDate1)
                
            Else
                If tmpDate1 <= CDate(DateValue(tmpDate1) & sBrkHrStart) Then
                    minTotal = minTotal + (CDec(CDate(DateValue(tmpDate1) & sBrkHrStart)) - CDec(tmpDate1))
                End If
                If tmpDate2 <= CDate(DateValue(tmpDate2) & sWorkHrEnd) Then
                    minTotal = minTotal + CDec(tmpDate2) - CDec(CDate(DateValue(tmpDate2) & sBrkHrEnd))
                End If
                
            End If
        End If
        
        
    Case DateDiff("d", CDate(DateValue(dt1)), CDate(DateValue(dt2))) = 1
        
        If InStr(1, "Saturday,Sunday", Format(dt1, "mmmm")) = 0 Then
            tmpDate1 = maxAmong(dt1, CDate(DateValue(dt1) & sWorkHrStart))
            If tmpDate1 <= CDate(DateValue(dt1) & sBrkHrStart) Then
                minTotal = minTotal + CDec(CDate(DateValue(tmpDate1) & sBrkHrStart)) - CDec(tmpDate1)
                minTotal = minTotal + (4# * hr1)
            End If
            If tmpDate1 >= CDate(DateValue(dt1) & sBrkHrStart) Then
                If tmpDate1 < CDate(DateValue(tmpDate1) & sWorkHrEnd) Then
                    minTotal = minTotal + CDec(CDate(DateValue(tmpDate1) & sWorkHrEnd)) - CDec(tmpDate1)
                Else
                    minTotal = minTotal + (4# * hr1)
                End If
            End If
        End If
        
        If InStr(1, "Saturday,Sunday", Format(dt2, "mmmm")) = 0 Then
            tmpDate2 = maxAmong(dt2, CDate(DateValue(dt2) & sWorkHrStart))
            If tmpDate2 <= CDate(DateValue(tmpDate2) & sBrkHrStart) Then
                minTotal = minTotal + CDec(tmpDate2) - CDec(CDate(DateValue(dt2) & sWorkHrStart))
            End If
            If tmpDate2 >= CDate(DateValue(dt2) & sBrkHrEnd) Then
                minTotal = minTotal + (4# * hr1)
                tmpDate2 = minAmong(tmpDate2, CDate(DateValue(tmpDate2) & sWorkHrEnd))
                minTotal = minTotal + CDec(tmpDate2) - CDec(CDate(DateValue(tmpDate2) & sBrkHrEnd))
            End If
        End If
    Case Else
        If InStr(1, "Saturday,Sunday", Format(dt1, "mmmm")) = 0 Then
            tmpDate1 = maxAmong(dt1, CDate(DateValue(dt1) & sWorkHrStart))
            If tmpDate1 <= CDate(DateValue(dt1) & sBrkHrStart) Then
                minTotal = minTotal + CDec(CDate(DateValue(tmpDate1) & sBrkHrStart)) - CDec(tmpDate1)
                minTotal = minTotal + (4# * hr1)
            End If
            If tmpDate1 >= CDate(DateValue(dt1) & sBrkHrStart) Then
                If tmpDate1 < CDate(DateValue(tmpDate1) & sWorkHrEnd) Then
                    minTotal = minTotal + CDec(CDate(DateValue(tmpDate1) & sWorkHrEnd)) - CDec(tmpDate1)
                Else
                    minTotal = minTotal + (4# * hr1)
                End If
            End If
        End If
        
        If InStr(1, "Saturday,Sunday", Format(dt2, "mmmm")) = 0 Then
            tmpDate2 = maxAmong(dt2, CDate(DateValue(dt2) & sWorkHrStart))
            If tmpDate2 <= CDate(DateValue(tmpDate2) & sBrkHrStart) Then
                minTotal = minTotal + CDec(tmpDate2) - CDec(CDate(DateValue(dt2) & sWorkHrStart))
            End If
            If tmpDate2 >= CDate(DateValue(dt2) & sBrkHrEnd) Then
                minTotal = minTotal + (4# * hr1)
                tmpDate2 = minAmong(tmpDate2, CDate(DateValue(tmpDate2) & sWorkHrEnd))
                minTotal = minTotal + CDec(tmpDate2) - CDec(CDate(DateValue(tmpDate2) & sBrkHrEnd))
            End If
        End If
        For dt = CDate(DateValue(dt1)) + 1 To CDate(DateValue(dt2)) - 1
            If InStr(1, "Saturday,Sunday", Format(dt2, "mmmm")) = 0 Then
                minTotal = minTotal + (8# * hr1)
            End If
        Next
    End Select
    
    If minTotal > 0 Then
        WorkHrs = Round(minTotal / Min1 / 60, 2)
    End If
End Function

'helper functions
Public Function minAmong(ParamArray p() As Variant) As Variant
    Dim pResult As Variant
    Dim i As Integer
    For i = 0 To UBound(p)
        If i = 0 Then
            pResult = p(i)
        Else
            If pResult > p(i) Then
                pResult = p(i)
            End If
        End If
    Next
    minAmong = pResult
End Function

Public Function maxAmong(ParamArray p() As Variant) As Variant
    Dim pResult As Variant
    Dim i As Integer
    For i = 0 To UBound(p)
        If i = 0 Then
            pResult = p(i)
        Else
            If pResult < p(i) Then
                pResult = p(i)
            End If
        End If
    Next
    maxAmong = pResult
End Function
 
Last edited:

Lilly420

Registered User.
Local time
Yesterday, 21:14
Joined
Oct 4, 2013
Messages
126
Good morning,

FIRST, THANK YOU so much for taking the time to write this code and helping me, I so appreciate your time and kindness.

It seems to work...except on a few date...below are the results of my testing:

Start: 7/30/2019 9:00AM - End: 7/30/2019 1:30PM = 3.5 - Correct
Start: 8/1/2019 8:00AM - End: 8/2/2019 9:00AM = 9 Correct
Start: 8/1/2019 9:00AM - End: 8/2/2019 1:00PM = 11 Correct
Start: 7/26/2019 10:45AM - End: 7/26/2019 11:20AM = 0.58 - should this be 0.35?
Start: 7/29/2019 2:00PM - End: 7/30/2019 9:00AM = 2 - should this be 4?

Again, I thank you for your time and patience with me.

Lilly
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:14
Joined
Sep 21, 2011
Messages
14,287
The 0.58 is correct as it is 35 mins / 60
The last I *think* should be 3, but is probably taking into account an hours lunch for each day perhaps?

Edit: for your last I get 4?

? workhrs(#29/07/2019 2 pm#,#30/07/2019 9 am#,"12 pm","1 pm")
4
 
Last edited:

Lilly420

Registered User.
Local time
Yesterday, 21:14
Joined
Oct 4, 2013
Messages
126
Oh, OK I see now, thank you. So I hate to ask this question, but would it be possible to adjust this code to do away with the 1 hour break and just make it a 8 hours...I apologize but I think that might better serve my situation or should we not do that? Again, thank you.
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:14
Joined
Sep 21, 2011
Messages
14,287
It is arne's code, so I will leave it up to him. I certainly do not want to muck it up, and having made a few tests myself, I believe it is working correctly.? My calculation of 3 hours was due to you saying previously 8 til 4, but arne's code defaults to 8 til 5 unless you supply differently, so I believe it is working correctly.?

Try supplying the same time for lunch start and end and see what that produces.
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:14
Joined
Sep 21, 2011
Messages
14,287
Lunchtime start and end of the same appears to work.

? workhrs(#29/07/2019 8:00 am#,#29/07/2019 5:00 pm#,,,"12 pm","12 pm")
9

Nifty piece of code.
Thank you arne
 

Lilly420

Registered User.
Local time
Yesterday, 21:14
Joined
Oct 4, 2013
Messages
126
Thank you for the help...I changed my query to what you have above...so I get 3 hours now, but shouldn't it be 4...7/29 I worked 2:00pm to 5:00pm which is 3 hours, then on 7/30 I worked 8:00AM to 9:00AM which is an additional hour, so in my mind that should = 4 hours total? And that is why maybe the break needs to be removed from the code? I don't know enough about code to tell--and this is so code is so awesome...and appreciate the help.
 

Gasman

Enthusiastic Amateur
Local time
Today, 02:14
Joined
Sep 21, 2011
Messages
14,287
Thank you for the help...I changed my query to what you have above...so I get 3 hours now, but shouldn't it be 4...7/29 I worked 2:00pm to 5:00pm which is 3 hours, then on 7/30 I worked 8:00AM to 9:00AM which is an additional hour, so in my mind that should = 4 hours total? And that is why maybe the break needs to be removed from the code? I don't know enough about code to tell--and this is so code is so awesome...and appreciate the help.

I get 4 hours from your dates and times, so I do not know what you are doing wrong?

Put the call to the function in the immediate window and copy and paste that and the result as I have done previously.

Code:
? workhrs(#29/07/2019 2 pm#,#30/07/2019 9 am#,,,"12 pm","1 pm")
 4 
? workhrs(#29/07/2019 2 pm#,#30/07/2019 9 am#)
 4
 

arnelgp

..forever waiting... waiting for jellybean!
Local time
Today, 09:14
Joined
May 7, 2009
Messages
19,237
sorry for the errors.
I tested again.
line 4 (0.58), I think is correct?
the difference is 35 minutes (35 min/60 min/hr = 0.58 hr).
only the last line is out of plumb.
here is the main function , pls retest again.
Code:
Public Function WorkHrs( _
                            dt1 As Date, _
                            dt2 As Date, _
                            Optional sWorkHrStart As String = "8 am", _
                            Optional sWorkHrEnd As String = "5 pm", _
                            Optional sBrkHrStart As String = "12 pm", _
                            Optional sBrkHrEnd As String = "1 pm") As Double
    Const hr1 As Double = 4.16666666715173E-02
    Const Min1 As Double = 6.94444444444442E-04
    
    Dim dStart As Date, dEnd As Date
    Dim minTotal As Double
    Dim tmpDate1 As Date, tmpDate2 As Date
    Dim dt As Date
    
    sWorkHrStart = " " & Trim(sWorkHrStart)
    sWorkHrEnd = " " & Trim(sWorkHrEnd)
    sBrkHrStart = " " & Trim(sBrkHrStart)
    sBrkHrEnd = " " & Trim(sBrkHrEnd)
    
    If dt1 > dt2 Then
        tmpDate1 = dt1
        dt1 = dt2
        dt2 = tmpDate1
    End If
    
    minTotal = 0#
    
    Select Case True
    Case DateValue(dt1) = DateValue(dt2)
    
        If InStr(1, "Saturday,Sunday", Format(dt1, "mmmm")) = 0 Then
            tmpDate1 = maxAmong(dt1, CDate(DateValue(dt1) & sWorkHrStart))
            tmpDate2 = minAmong(dt2, CDate(DateValue(dt2) & sWorkHrEnd))
            
            If CDbl(tmpDate2) - CDbl(tmpDate1) <= (4 * hr1) Then
                minTotal = CDec(tmpDate2) - CDec(tmpDate1)
                
            Else
                If tmpDate1 <= CDate(DateValue(tmpDate1) & sBrkHrStart) Then
                    minTotal = minTotal + (CDec(CDate(DateValue(tmpDate1) & sBrkHrStart)) - CDec(tmpDate1))
                End If
                If tmpDate2 <= CDate(DateValue(tmpDate2) & sWorkHrEnd) Then
                    minTotal = minTotal + CDec(tmpDate2) - CDec(CDate(DateValue(tmpDate2) & sBrkHrEnd))
                End If
                
            End If
        End If
        
        
    Case DateDiff("d", CDate(DateValue(dt1)), CDate(DateValue(dt2))) = 1
        
        If InStr(1, "Saturday,Sunday", Format(dt1, "mmmm")) = 0 Then
            tmpDate1 = maxAmong(dt1, CDate(DateValue(dt1) & sWorkHrStart))
            If tmpDate1 <= CDate(DateValue(dt1) & sBrkHrStart) Then
                minTotal = minTotal + CDec(CDate(DateValue(tmpDate1) & sBrkHrStart)) - CDec(tmpDate1)
                minTotal = minTotal + (4# * hr1)
            End If
            If tmpDate1 >= CDate(DateValue(dt1) & sBrkHrStart) Then
                If tmpDate1 < CDate(DateValue(tmpDate1) & sWorkHrEnd) Then
                    minTotal = minTotal + CDec(CDate(DateValue(tmpDate1) & sWorkHrEnd)) - CDec(tmpDate1)
                Else
                    minTotal = minTotal + (4# * hr1)
                End If
            End If
        End If
        
        If InStr(1, "Saturday,Sunday", Format(dt2, "mmmm")) = 0 Then
            tmpDate2 = maxAmong(dt2, CDate(DateValue(dt2) & sWorkHrStart))
            If tmpDate2 <= CDate(DateValue(dt2) & sBrkHrStart) Then
                minTotal = minTotal + CDec(tmpDate2) - CDec(CDate(DateValue(tmpDate2) & sWorkHrStart))
            End If
            If tmpDate2 >= CDate(DateValue(dt2) & sBrkHrStart) Then
                If tmpDate2 < CDate(DateValue(tmpDate1) & sWorkHrEnd) Then
                    minTotal = minTotal + CDec(tmpDate2) - CDec(CDate(DateValue(tmpDate2) & sBrkHrEnd))
                Else
                    minTotal = minTotal + (4# * hr1)
                End If
            End If
        End If
    Case Else
        If InStr(1, "Saturday,Sunday", Format(dt1, "mmmm")) = 0 Then
            tmpDate1 = maxAmong(dt1, CDate(DateValue(dt1) & sWorkHrStart))
            If tmpDate1 <= CDate(DateValue(dt1) & sBrkHrStart) Then
                minTotal = minTotal + CDec(CDate(DateValue(tmpDate1) & sBrkHrStart)) - CDec(tmpDate1)
                minTotal = minTotal + (4# * hr1)
            End If
            If tmpDate1 >= CDate(DateValue(dt1) & sBrkHrStart) Then
                If tmpDate1 < CDate(DateValue(tmpDate1) & sWorkHrEnd) Then
                    minTotal = minTotal + CDec(CDate(DateValue(tmpDate1) & sWorkHrEnd)) - CDec(tmpDate1)
                Else
                    minTotal = minTotal + (4# * hr1)
                End If
            End If
        End If
        
        If InStr(1, "Saturday,Sunday", Format(dt2, "mmmm")) = 0 Then
            tmpDate2 = maxAmong(dt2, CDate(DateValue(dt2) & sWorkHrStart))
            If tmpDate2 <= CDate(DateValue(dt2) & sBrkHrStart) Then
                minTotal = minTotal + CDec(tmpDate2) - CDec(CDate(DateValue(tmpDate2) & sWorkHrStart))
            End If
            If tmpDate2 >= CDate(DateValue(dt2) & sBrkHrStart) Then
                If tmpDate2 < CDate(DateValue(tmpDate1) & sWorkHrEnd) Then
                    minTotal = minTotal + CDec(tmpDate2) - CDec(CDate(DateValue(tmpDate2) & sBrkHrEnd))
                Else
                    minTotal = minTotal + (4# * hr1)
                End If
            End If
        End If
        For dt = CDate(DateValue(dt1)) + 1 To CDate(DateValue(dt2)) - 1
            If InStr(1, "Saturday,Sunday", Format(dt2, "mmmm")) = 0 Then
                minTotal = minTotal + (8# * hr1)
            End If
        Next
    End Select
    
    If minTotal > 0 Then
        WorkHrs = Round(minTotal / Min1 / 60, 2)
    End If
End Function
 

Users who are viewing this thread

Top Bottom