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

Posted on 2006-05-24
Last Modified: 2012-06-27
Does anybody have any C# code that will count the number of workdays between two dates and exclude a list of holidays and weekends?
Question by:defranz
    LVL 6

    Accepted Solution


    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,

    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(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())
                    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;
                return c;
    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.

    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(end_day == DayOfWeek.Saturday || start_day == DayOfWeek.Saturday)
       else if (startDate.Date > endDate.Date)
          non_bus_days = non_bus_days + 2;

    // total business days
    return date_diff_in_days - non_bus_days;

    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

    Then add:

    int num_holidays = 0;

    foreach(DateTime holiday in holidayList)
      if(holiday.Date >= startDate.Date && holiday.Date <= endDate.Date)

    Then change the return line to be:

    // total business days
    return date_diff_in_days - non_bus_days - num_holidays;

    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!

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Article by: Ivo
    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 (, 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…

    728 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now