Question DateDiff Business Days Only

raccam1976

New member
Joined
Nov 20, 2008
Messages
1
Programming Experience
1-3
If dr.Item("approvaloutemail") = "1/1/1800" Then
approvaldate = drMaster.Item("lmapprovaldate")
ctc = drMaster.Item("dateallcondcleared")
If (DateDiff(DateInterval.DayOfYear, Today, approvaldate) >= -5 And _
DateDiff(DateInterval.DayOfYear, Today, approvaldate) <= -3) Then
If ctc = "1/1/1800" Then

"sendFormattedEmail"

--- The Question
I only need to count business days (monday thru friday), doesn't matter about holidays.

Can someone please point me in the right direction?

thank you
 
VB.NET:
Private Function GetBusinessDayDifference(ByVal date1 As Date, ByVal date2 As Date) As Integer
    Dim startDate As Date = If(date1 < date2, date1, date2)
    Dim endDate As Date = If(date1 > date2, date1, date2)
    Dim difference As TimeSpan = endDate - startDate
    Dim totalDays As Integer = difference.Days
    Dim weeks As Integer = totalDays \ 7
    Dim days As Integer = totalDays Mod 7
    Dim businessDays As Integer = weeks * 5

    For offset As Integer = 1 To days
        Select Case startDate.AddDays(offset).DayOfWeek
            Case DayOfWeek.Monday, _
                 DayOfWeek.Tuesday, _
                 DayOfWeek.Wednesday, _
                 DayOfWeek.Thursday, _
                 DayOfWeek.Friday
                businessDays += 1
        End Select
    Next

    Return businessDays
End Function
That loop might some slight adjustments but that's basically it.
 
Hmmm... I just noticed that you're using VB.NET 2003. You'll need to change this:
VB.NET:
Dim startDate As Date = If(date1 < date2, date1, date2)
to this:
VB.NET:
Dim startDate As Date = CDate(IIf(date1 < date2, date1, date2))
 
I was thinking something like:

VB.NET:
Dim bd as Integer = 0
While startDate < endDate
  If startDate.DayOfWeek <> DayOfWeek.Saturday AndAlso startDate.DayOfWeek <> DayOfWeek.Sunday Then bd+= 1
  startDate = startDate.AddDays(1)
End While
Return bd
jmc's code is likely faster
 
Back
Top