VBcoders Guest



Don't have an account yet? Register
 


Forgot Password?



Function returns the number of business days between two dates.

by Tim Van Steenburgh (2 Submissions)
Category: Miscellaneous
Compatability: Visual Basic 4.0 (32-bit)
Difficulty: Unknown Difficulty
Originally Published: Mon 5th April 1999
Date Added: Mon 8th February 2021
Rating: (1 Votes)

Function returns the number of business days between two dates.

Rate Function returns the number of business days between two dates.




If DateDiff("d", date1, date2) < 0 Then ' Date1 is after Date2
    BusinessDaysApart = -1
Else ' Date 1 is before Date2
    If DateDiff("ww", date1, date2) = 0 Then ' Date1 and Date2 are in same week
        
        If (Weekday(date1) >= 2 And Weekday(date1) <= 6) And (Weekday(date2) <= 6 And Weekday(date2) >= 2) Then 'Date1 and Date2 are between Mon and Fri
            BusinessDaysApart = DateDiff("d", date1, date2)
        Else ' either Date1 or Date2 is on the weekend
            If DateDiff("d", date1, date2) > 4 Then ' Date1 and Date2 are more than 5 literal days apart
                BusinessDaysApart = 4
                ' Date1 and Date2 can be a maximum of 4 business days apart if Date1 and Date2 are
                ' in the same week
            Else
                BusinessDaysApart = DateDiff("d", date1, date2)
            End If
        End If
    
    ElseIf DateDiff("ww", date1, date2) > 0 Then ' Date1 and Date2 are not in the same week
                    
        BusinessDaysApart = DateDiff("d", date1, date2) - (DateDiff("ww", date1, date2) * 2)
        ' (subtract 2 days (weekends) from total literal days separating Date1 and Date2
        '  for every week separating Date1 and Date2)
    End If
    
End If

End Function

Download this snippet    Add to My Saved Code

Function returns the number of business days between two dates. Comments

No comments have been posted about Function returns the number of business days between two dates.. Why not be the first to post a comment about Function returns the number of business days between two dates..

Post your comment

Subject:
Message:
0/1000 characters