Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / web / ASP.NET

Find the count of a weekday between two dates without iterating/looping

5.00/5 (3 votes)
10 Nov 2011CPOL 16.2K  
How about this?public static class ExtendDateTime{ public int CountWeekDays(this DateTime thisdate, DateTime thatDate) { int days = Math.Abs((thisDate - thatDate).Days) + 1; return = ((days/7) * 5) + (days % 7); }}Usage would be like this:DateTime...
How about this?
C#
public static class ExtendDateTime
{
    public int CountWeekDays(this DateTime thisdate, DateTime thatDate)
    {
        int days = Math.Abs((thisDate - thatDate).Days) + 1;
        return = ((days/7) * 5) + (days % 7);
    }
}

Usage would be like this:
C#
DateTime startDate = new DateTime(2011, 11, 1);
int weekdays = startDate.CountWeekDays(new DateTime(2011, 11, 30));


BTW, if you haven't noticed, this extension method works whether the date being compared occurs in the future OR in the past.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)