/// <summary>
/// Calculates number of business days, taking into account:
/// - weekends (Saturdays and Sundays)
/// - bank holidays in the middle of the week
/// </summary>
/// <param name="firstDay">First day in the time interval</param>
/// <param name="lastDay">Last day in the time interval</param>
/// <param name="bankHolidays">List of bank holidays excluding weekends</param>
/// <returns>Number of business days during the 'span'</returns>
public static int BusinessDaysUntil(this DateTime firstDay, DateTime lastDay, params DateTime[] bankHolidays)
{
firstDay = firstDay.Date;
lastDay = lastDay.Date;
if (firstDay > lastDay)
throw new ArgumentException("Incorrect last day " + lastDay);
TimeSpan span = lastDay - firstDay;
int businessDays = span.Days + 1;
int fullWeekCount = businessDays / 7;
// find out if there are weekends during the time exceedng the full weeks
if (businessDays > fullWeekCount*7)
{
// we are here to find out if there is a 1-day or 2-days weekend
// in the time interval remaining after subtracting the complete weeks
int firstDayOfWeek = (int) firstDay.DayOfWeek;
int lastDayOfWeek = (int) lastDay.DayOfWeek;
if (lastDayOfWeek < firstDayOfWeek)
lastDayOfWeek += 7;
if (firstDayOfWeek <= 6)
{
if (lastDayOfWeek >= 7)// Both Saturday and Sunday are in the remaining time interval
businessDays -= 2;
else if (lastDayOfWeek >= 6)// Only Saturday is in the remaining time interval
businessDays -= 1;
}
else if (firstDayOfWeek <= 7 && lastDayOfWeek >= 7)// Only Sunday is in the remaining time interval
businessDays -= 1;
}
// subtract the weekends during the full weeks in the interval
businessDays -= fullWeekCount + fullWeekCount;
// subtract the number of bank holidays during the time interval
foreach (DateTime bankHoliday in bankHolidays)
{
DateTime bh = bankHoliday.Date;
if (firstDay <= bh && bh <= lastDay)
--businessDays;
}
return businessDays;
}
I sucked that answer in the O.R.. She has the advantage that she can spend holidays. remembering that holiday is something that should be obtained from another source and may vary according to the municipality/state (not necessarily yours) and in which year is.
Behold working in the ideone. And in the .NET Fiddle. Also put on the Github for future reference.
have some examples here
– Marco Souza
Since you already have excellent answers, I’ll just leave this package who works with holidays, including.
– Randrade
@Randrade do you use? Did you see something like it takes the holidays? Does it for the future? Until when? Deals with regional holidays?
– Maniero
@bigown I no longer use because I have no need, but I’ve used and have systems using to date (with adaptations). The holidays you add in this class. Do yes for the future, with the same holidays defined. Until you want, but will always be on the same dates. It deals with regional holidays, but you need to add them in the class. If you have any more questions, call in the chat that explain better.
– Randrade
@bigown You can see an example here
– Randrade
The question is not about holidays, so it is a duplicate of a previous question.
– Leonel Sanches da Silva