Is their a method in TimeSpan or Time function that...

  • Thread starter Thread starter Lucas Sain
  • Start date Start date
L

Lucas Sain

Hi,

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.

How can this be done? Is their a method to do it... Any ideas are
appreciated,

Thanks Ahead
Lucas
 
Lucas Sain said:
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
 
Carl,

Thanks, look like a solution.

Regards
Lucas
Carl Prothman said:
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)), _
 
Back
Top