First I feel I should warn you that computation about time are hard, damn hard. You probably want to use a library specifically designed to address it like Noda Time. For instance, your code takes into account weekends assuming a Monday to Friday work week (it is not the case everywhere in the world) and does not take into account bank holidays, which are not working days.
Said that, I think that your code is ok but I would rewrite it in this way to make it clearer. I'd like to split the code that computes the working days in full weeks and the code that computes the day in partial weeks.
I also don't like the casting you do too much in the for loop.
You can avoid the for loop altogether if you think a bit about how you should handle remaining days and do the maths. If the logic I'm following is not clear from the code leave a comment and I'll explain it a bit further.
private static int GetNumberOfWorkingDays(DateTime start, DateTime stop)
var days = (stop - start).Days + 1;
return workDaysInFullWeeks(days) + workDaysInPartialWeek(start.DayOfWeek, days);
private static int workDaysInFullWeeks(int totalDays)
return (totalDays / 7) * 5;
private static int workDaysInPartialWeek(DayOfWeek firstDay, int totalDays)
var remainingDays = totalDays % 7;
var daysToSaturday = (int) DayOfWeek.Saturday - (int) firstDay;
if(remainingDays <= daysToSaturday)
/* daysToSaturday are the days before the weekend,
* the rest of the expression computes the days remaining after we
* ignore Saturday and Sunday
// Range ends in a Saturday or in a Sunday
if (remainingDays <= daysToSaturday + 2)
// Range ends after a Sunday
return remainingDays - 2;
Note that you need to include both the start and stop dates but
TimeSpan interval = stop - start; returns the difference between these two dates causing an off by one error (Credits to mjolka for noticing it). For instance
new DateTime(2014, 10, 13) - new DateTime(2014, 10, 12) is one day but you want to include both these dates so we should add one to the result.