Connecting Tech Pros Worldwide Forums | Help | Site Map

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

Lucas Sain
Guest
 
Posts: n/a
#1: Jul 19 '05
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



Carl Prothman [MVP]
Guest
 
Posts: n/a
#2: Jul 19 '05

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


"Lucas Sain" <lsain@lidersoft.com> wrote[color=blue]
> 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.
>[/color]

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




Lucas Sain
Guest
 
Posts: n/a
#3: Jul 19 '05

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


Carl,

Thanks, look like a solution.

Regards
Lucas
"Carl Prothman [MVP]" <carlpr@spamcop.net> wrote in message
news:efDbdikXDHA.2620@TK2MSFTNGP09.phx.gbl...[color=blue]
> "Lucas Sain" <lsain@lidersoft.com> wrote[color=green]
> > 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[/color][/color]
pasted[color=blue][color=green]
> > from August 4(Monday) to August 10(Sunday) should return 5 days and not[/color][/color]
7.[color=blue][color=green]
> >[/color]
>
> Here is one way....
>
> Given the following function:
>
> Private Function GetNonWeekendDayTotal(ByVal StartDate As DateTime,[/color]
ByVal EndDate As DateTime) As Integer[color=blue]
>
> Dim totalWeekDays As Integer = 0
>
> For i As Integer = 1 To EndDate.Subtract(StartDate).Days
> If StartDate.AddDays(i).DayOfWeek() <> DayOfWeek.Saturday And[/color]
_[color=blue]
> 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[/color]
GregorianCalendar(GregorianCalendarTypes.USEnglish )), _[color=blue]
> New DateTime(2002, 8, 10, New[/color]
GregorianCalendar(GregorianCalendarTypes.USEnglish )))[color=blue]
>
> 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
>
>
>
>[/color]


Closed Thread


Similar .NET Framework bytes