65.9K
CodeProject is changing. Read more.
Home

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

starIconstarIconstarIconstarIconstarIcon

5.00/5 (3 votes)

Nov 10, 2011

CPOL
viewsIcon

16150

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?
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 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.