"Lucas Sain" <ls***@lidersoft.com> wrote
How can I return the Total days between 2 dates but have it only sum the
WeekDays and not Weekends. for example Lets say: How many days have pasted
from August 4(Monday) to August 10(Sunday) should return 5 days and not 7.
Here is one way....
Given the following function:
Private Function GetNonWeekendDayTotal(ByVal StartDate As DateTime, ByVal EndDate As DateTime) As Integer
Dim totalWeekDays As Integer = 0
For i As Integer = 1 To EndDate.Subtract(StartDate).Days
If StartDate.AddDays(i).DayOfWeek() <> DayOfWeek.Saturday And _
StartDate.AddDays(i).DayOfWeek() <> DayOfWeek.Sunday Then
totalWeekDays += 1
End If
Next
Return totalWeekDays
End Function
Call it as follows:
Dim i As Integer = GetNonWeekendDayTotal( _
New DateTime(2002, 8, 4, New GregorianCalendar(GregorianCalendarTypes.USEnglish )), _
New DateTime(2002, 8, 10, New GregorianCalendar(GregorianCalendarTypes.USEnglish )))
For the GregorialCalendar class, you'll need to the following import:
Imports System.Globalization
--
Thanks,
Carl Prothman
Microsoft ASP.NET MVP
http://www.able-consulting.com