Caclulate Weekdays between two dates
A way to calculate how many weekdays are there between two dates.
1: public static int GetWeekDays(DateTime startDate, DateTime endDate)
2: {
3: startDate = startDate.Date;
4: endDate = endDate.Date;
5:
6: if (endDate <= startDate)
7: return 0;
8:
9: TimeSpan ts = endDate - startDate;
10: int wholeWeeks = ts.Days / 7;
11: int result = wholeWeeks * 5;
12:
13: DateTime fractionalStartDate = startDate.AddDays(wholeWeeks * 7);
14: int loop = ts.Days % 7;
15: while (loop > 0)
16: {
17: if (((int)fractionalStartDate.DayOfWeek) % 6 > 0)
18: result++;
19:
20: fractionalStartDate = fractionalStartDate.AddDays(1);
21: loop--;
22: }
23:
24: return result;
25: }
