Hi,

I am new to c#. Now i m currently working in a leave system project, in this i have two popup calendar to select the leave-start date and leave-end date for employee. If employee select more than 5 days, weekend dates should not be calculate as a leave credit. I m using the following code to find the difference.

DateTime dt = Convert.ToDateTime(txtfrmdate.SelectedDate.ToShortDateString());
        DateTime dt1 = Convert.ToDateTime(txtenddate.SelectedDate.ToShortDateString());
        TimeSpan diff = dt1.Subtract(dt);
        int days = diff.Days + 1;

In this if we select dates 11/26/2009 to 12/2/2009, result will be 7. here the weekend dates also calculated. But i want to ignore the weekend days.

Please can any one help me how to do this.

Dani AI

Generated

The solutions from @RamesenS and correctly solve the problem by iterating day‑by‑day and counting weekdays. That is simple and easy to verify, but for very large ranges a constant‑time approach is faster and often clearer when also needing to exclude company holidays. The implementation below counts full weeks (5 workdays each) then handles the leftover days (at most 6 checks), and optionally subtracts any weekday holidays. It treats start and end as date‑only and is inclusive; it also swaps inputs if the dates are passed in the wrong order.

public static int BusinessDaysBetween(DateTime start, DateTime end, HashSet<DateTime> holidays = null)
{
    if (start > end) { var t = start; start = end; end = t; }

    start = start.Date;
    end = end.Date;

    int totalDays = (int)(end - start).TotalDays + 1; // inclusive
    int fullWeeks = totalDays / 7;
    int businessDays = fullWeeks * 5;
    int remainder = totalDays % 7;

    int startDow = (int)start.DayOfWeek; // Sunday=0 .. Saturday=6
    for (int i = 0; i < remainder; i++)
    {
        int dow = (startDow + i) % 7;
        if (dow != 0 && dow != 6) businessDays++;
    }

    if (holidays != null)
    {
        foreach (var h in holidays)
            if (h.Date >= start && h.Date <= end && h.DayOfWeek != DayOfWeek.Saturday && h.DayOfWeek != DayOfWeek.Sunday)
                businessDays--;
    }

    return businessDays;
}

Notes: the function is inclusive (both start and end counted) — change the + 1 if an exclusive range is required. Holidays should be stored as date‑only values (no time) and only subtracted when they fall on a weekday. For leave policies that allow partial days, switch to a fractional return type and apply policy rules separately. For the example in the original post (11/26/2009–12/2/2009) this method returns five working days (weekend excluded).

Recommended Answers

All 4 Replies

Try this.

DateTime startDate = new DateTime( 2004 , 3 , 18 ) ;
                  DateTime endDate   = new DateTime( 2004 , 3 , 22 ) ;

                  int countDays = 0;

                  DateTime dateIterator = startDate ;

                  while( dateIterator < endDate.AddDays(1) )
                  {
                        if( dateIterator.DayOfWeek != DayOfWeek.Saturday && dateIterator.DayOfWeek != DayOfWeek.Sunday )
                              countDays ++;

                        dateIterator = dateIterator.AddDays(1);
                  }

                  Console.Out.WriteLine( "Number of working days between {0} and {1}\nis {2} days" , startDate , endDate , countDays );
                  Console.Out.WriteLine( "Press Return To Finish" );
                  Console.In.Read();

Here change the start date and end dates accordingly.

private int DaysIgnoreWeekends(DateTime dtst, DateTime dtend)
        { 
            TimeSpan days = dtend.Subtract(dtst);
            int count = 0;
            for (int a = 0; a < days.Days + 1; a++)
            {
                if (dtst.DayOfWeek != DayOfWeek.Saturday && dtst.DayOfWeek != DayOfWeek.Sunday)
                {
                    count++;
                }
                dtst = dtst.AddDays(1.0);
            }
            return count;
        }

this is a little function

oops sorry about that i didn't notice that mr. ramesh post a really nice solution. anyway i think you have the point.

private int DaysIgnoreWeekends(DateTime dtst, DateTime dtend)
        { 
            TimeSpan days = dtend.Subtract(dtst);
            int count = 0;
            for (int a = 0; a < days.Days + 1; a++)
            {
                if (dtst.DayOfWeek != DayOfWeek.Saturday && dtst.DayOfWeek != DayOfWeek.Sunday)
                {
                    count++;
                }
                dtst = dtst.AddDays(1.0);
            }
            return count;
        }

this is a little function

Hi,
Thank you very much, this code works properly

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.