• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1251
  • Last Modified:

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

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
defranz
Asked:
defranz
  • 2
1 Solution
 
neilpriceCommented:
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
            Console.ReadLine();
        }

        //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())
            {
                a = a.AddDays(1);
                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
 
mrichmonCommented:
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.

int BusinessDateDiff(DateTime startDate, DateTime endDate)
{

// 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;
}

// total business days
return date_diff_in_days - non_bus_days;

}
0
 
mrichmonCommented:
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

Then add:

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:

// total business days
return date_diff_in_days - non_bus_days - num_holidays;
0
 
defranzAuthor Commented:
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

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now