In C#, how can I calculate the number of business (or weekdays) days between two dates?
This is a generic solution.
startdayvalue is day number of start date.
weekendday_1 is day numner of week end.
day number - MON - 1, TUE - 2, ... SAT - 6, SUN -7.
difference is difference between two dates..
Example : Start Date : 4 April, 2013, End Date : 14 April, 2013
Difference : 10, startdayvalue : 4, weekendday_1 : 7 (if SUNDAY is a weekend for you.)
This will give you number of holidays.
No of business day = (Difference + 1) - holiday1
if (startdayvalue > weekendday_1)
{
if (difference > ((7 - startdayvalue) + weekendday_1))
{
holiday1 = (difference - ((7 - startdayvalue) + weekendday_1)) / 7;
holiday1 = holiday1 + 1;
}
else
{
holiday1 = 0;
}
}
else if (startdayvalue < weekendday_1)
{
if (difference > (weekendday_1 - startdayvalue))
{
holiday1 = (difference - (weekendday_1 - startdayvalue)) / 7;
holiday1 = holiday1 + 1;
}
else if (difference == (weekendday_1 - startdayvalue))
{
holiday1 = 1;
}
else
{
holiday1 = 0;
}
}
else
{
holiday1 = difference / 7;
holiday1 = holiday1 + 1;
}