How to get the number of days between two dates, excluding weekends?

Posted on 2006-05-18
Last Modified: 2007-12-19
Hello all,

I've searched and a found a couple of suggestions that didn't work for me.

I can get the amount of days with the weekends included in the count. I can't seem to figure out how to get the number of days without the weekends.

DateTime dt1 = Convert.ToDateTime(FirstDayOut.Text);
DateTime dt2 = Convert.ToDateTime(WillReturn.Text);
TimeSpan ts = dt2.Subtract(dt1);
int days = ts.Days;
txtDate.Text = days.ToString();

This is very urgent.
Any help would be great.

Question by:JordanBlackler
    LVL 28

    Expert Comment

    I haven't worked out the exact code, but you can do something like this:

    System.DayOfTheWeek day1 = dt1.DayOfTheWeek;
    System.DayOfTheWeek day2 = dt2.DayOfTheWeek;

    then you need to figure out from the number of days, and the day or the week you start and stop how many weekend days are between there.
    LVL 8

    Expert Comment

    May be this one will work for you:

                TimeSpan ts = dt2 - dt1;
                int totalDays = (int)ts.TotalDays;
                int fullWeeks = totalDays / 7;
                int daysRest = totalDays - fullWeeks * 7;
                totalDays -= fullWeeks * 2;

                DateTime day = dt2.AddDays(-daysRest);
                for (int i=0; i<daysRest; i++)
                    if (day.DayOfWeek == DayOfWeek.Saturday || day.DayOfWeek == DayOfWeek.Sunday)
                    day = day.AddDays(1);


    Just be careful with times of your dates.

    LVL 35

    Accepted Solution

    Maybe this would help you out.

    I just converted on the fly from COld Fusion (there I also have holidays in a text file which I can add to this if you need to account for that 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 == 7 || start_day == 7)
       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

    SInce I just converted on the fly as I typed you may need to touch up a bit, but the algorithm is sound.
    LVL 35

    Expert Comment

    For example this line:
    if(end_day == 7 || start_day == 7)

    WOuld be better as:

    if(end_day == DayOfWeek.Saturday || start_day == DayOfWeek.Saturday)

    Expert Comment

    I have a better and simple code
    here it goes

    public static  int BusinessDateDiff(DateTime startDate, DateTime endDate)
                int dayscount = 0;

                DateTime BDate = startDate;

                while (BDate <= endDate)
                    if(BDate.DayOfWeek != DayOfWeek.Sunday)
                    BDate = BDate.AddDays(1);

                return dayscount;
    LVL 35

    Expert Comment

    nice try, but it is not "better and simpler" as it gives the wrong answer, doesn't account for holidays, and the whole point was to calculate NOT loop.

    Featured Post

    Courses: Start Training Online With Pros, Today

    Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

    Join & Write a Comment

    Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
    Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…
    This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

    746 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

    18 Experts available now in Live!

    Get 1:1 Help Now