Solved

# How to count the number of workdays between two dates and exclude holidays and weekends

Posted on 2006-05-24
1,226 Views
Does anybody have any C# code that will count the number of workdays between two dates and exclude a list of holidays and weekends?
0
Question by:defranz

LVL 6

Accepted Solution

Hi,

This is something crude I just knocked up for you.
Pass in the two DateTime variables into the GetWorkingDays function along with a list of Holidays you need to create (shown in code).  Weekends are automatically excluded.

Hope this helps,
Neil

using System;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication2
{
class Program
{
static void Main(string[] args)
{
DateTime dt1 = new DateTime(1994,12,12,12,11,33); // Start date Dec 12th
DateTime dt2 = new DateTime(1994,12,31,13,56,11); // End date Dec 31st
List<DateTime> holidays = new List<DateTime>();
holidays.Add(new DateTime(1994, 12, 14)); // Make Dec 14th a holiday
Console.WriteLine(dt1.Date.ToShortDateString());
Console.WriteLine(GetWorkingDays(dt1, dt2).ToString()); // without holidays
Console.WriteLine(GetWorkingDays(dt1, dt2, holidays).ToString()); // with holidays
}

//These crude methods assumes that DateTime b is later than DateTime a
//If not they will just quit out and return 0
public static int GetWorkingDays(DateTime a, DateTime b)
{
return GetWorkingDays(a, b, new List<DateTime>());
}
public static int GetWorkingDays(DateTime a, DateTime b, List<DateTime> holidays)
{
if (a > b) return 0;
int c = 0;
while (a.Date.ToShortDateString() != b.Date.ToShortDateString())
{
if (!(a.DayOfWeek == DayOfWeek.Saturday || a.DayOfWeek == DayOfWeek.Sunday))
{
bool isNotHoliday = true;
foreach (DateTime d in holidays)
{
if (d.ToShortDateString() == a.ToShortDateString())
isNotHoliday = false;
}
if(isNotHoliday)
c++;
}
}
return c;
}
}
}
0

LVL 35

Expert Comment

I just asnwered this the other day :o)

Here is a function I wrote that does this - I adapted from a COld Fusion version I had written.  Then it can be modified to read holidays from a database or text file so those days are excluded as well.

{

// Local variables for date computations
int start_day = startDate.DayOfWeek;
int end_day = endDate.DayOfWeek;
int date_diff_in_days = (((TimeSpan)(endDate - startDate)).Days;
int whole_weeks = date_diff_in_days\7;
int days_left = date_diff_in_days %7;

// Subtract weekends in whole weeks
int non_bus_days = whole_weeks * 2;

// Subtract partial or whole weekends for partial weeks
if(days_left)
{
if(end_day == DayOfWeek.Saturday || start_day == DayOfWeek.Saturday)
non_bus_days++;
else if (startDate.Date > endDate.Date)
non_bus_days = non_bus_days + 2;
}

return date_diff_in_days - non_bus_days;

}
0

LVL 35

Expert Comment

To add in holidays create an overload that takes in an ArrayList of DateTime objects (.Net 1.1) or a List<DateTime> (.Net 2).  Assume that is stored in a variable called holidayList

int num_holidays = 0;

foreach(DateTime holiday in holidayList)
{
if(holiday.Date >= startDate.Date && holiday.Date <= endDate.Date)
num_holidays++;
}

Then change the return line to be:

return date_diff_in_days - non_bus_days - num_holidays;
0

Author Comment

Thanks for the quick response neilprice.

Sorry for my slow response. That worked for what I needed. I couldn't believe it was that easy when I looked at the code. I was having a serious brain cramp yesterday.

Thanks again!
0

## Featured Post

C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
how to add IIS SMTP to handle application/Scanner relays into office 365.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

#### Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!