Difference between two dates, ignoring weekend - working days only [duplicate]

4

I was looking for something similar, but unfortunately I did not find anything.

I found this solution that helped me a lot but it's in Java. How to calculate the difference between two dates by ignoring the weekends in Java without using loop

I would like in C #.

How to return days between 2 dates by ignoring the weekends. Just the weekdays?

    
asked by anonymous 14.06.2016 / 19:04

4 answers

5

I ended up doing this code that works very well, I hope I can help other people.

   public static int GetDifDias(DateTime initialDate, DateTime finalDate)
        {
            var days = 0;
            var daysCount = 0;
            days = initialDate.Subtract(finalDate).Days;

            if (days < 0)
                days = days * -1;

            for (int i = 1; i <= days; i++)
            {
                initialDate = initialDate.AddDays(1);

                if (initialDate.DayOfWeek != DayOfWeek.Sunday &&
                    initialDate.DayOfWeek != DayOfWeek.Saturday)
                    daysCount++;
            }
            return daysCount;
        }
    
14.06.2016 / 19:07
4

Yet another way to join the emerson-js lean solution with the @bigown solution that includes holidays:

public static int CountDiasUteis(DateTime d1, DateTime d2, params DateTime[] bankHolidays)
{
    List<DateTime> datas = new List<DateTime>();

    for (DateTime i = d1.Date; i <= d2.Date; i = i.AddDays(1))
    {
        datas.Add(i);
    }

    return datas.Count(d => d.DayOfWeek != DayOfWeek.Saturday &&
                            d.DayOfWeek != DayOfWeek.Sunday &&
                            !bankHolidays.Contains(d));
}
    
14.06.2016 / 20:30
4
/// <summary>
/// Calculates number of business days, taking into account:
///  - weekends (Saturdays and Sundays)
///  - bank holidays in the middle of the week
/// </summary>
/// <param name="firstDay">First day in the time interval</param>
/// <param name="lastDay">Last day in the time interval</param>
/// <param name="bankHolidays">List of bank holidays excluding weekends</param>
/// <returns>Number of business days during the 'span'</returns>
public static int BusinessDaysUntil(this DateTime firstDay, DateTime lastDay, params DateTime[] bankHolidays)
{
    firstDay = firstDay.Date;
    lastDay = lastDay.Date;
    if (firstDay > lastDay)
        throw new ArgumentException("Incorrect last day " + lastDay);

    TimeSpan span = lastDay - firstDay;
    int businessDays = span.Days + 1;
    int fullWeekCount = businessDays / 7;
    // find out if there are weekends during the time exceedng the full weeks
    if (businessDays > fullWeekCount*7)
    {
        // we are here to find out if there is a 1-day or 2-days weekend
        // in the time interval remaining after subtracting the complete weeks
        int firstDayOfWeek = (int) firstDay.DayOfWeek;
        int lastDayOfWeek = (int) lastDay.DayOfWeek;
        if (lastDayOfWeek < firstDayOfWeek)
            lastDayOfWeek += 7;
        if (firstDayOfWeek <= 6)
        {
            if (lastDayOfWeek >= 7)// Both Saturday and Sunday are in the remaining time interval
                businessDays -= 2;
            else if (lastDayOfWeek >= 6)// Only Saturday is in the remaining time interval
                businessDays -= 1;
        }
        else if (firstDayOfWeek <= 7 && lastDayOfWeek >= 7)// Only Sunday is in the remaining time interval
            businessDays -= 1;
    }

    // subtract the weekends during the full weeks in the interval
    businessDays -= fullWeekCount + fullWeekCount;

    // subtract the number of bank holidays during the time interval
    foreach (DateTime bankHoliday in bankHolidays)
    {
        DateTime bh = bankHoliday.Date;
        if (firstDay <= bh && bh <= lastDay)
            --businessDays;
    }

    return businessDays;
}

I picked up this answer in the SO . It has the advantage that it can spend holidays. remembering that holiday is something that should be obtained from another source and may vary according to the county / state (not necessarily yours) and in what year it is.

See running on DotNetFiddle and on CodingGround .

    
14.06.2016 / 19:08
3

Friends, just one more way to do it:

public static int CountDiasUteis(DateTime d1, DateTime d2) {

   int fator = (d1 > d2 ? -1: 1);

   List<DateTime> datas = new List<DateTime>();

   for (DateTime i = d1.Date; (fator==-1 ? i >= d2.Date : i <= d2.Date); i = i.AddDays(fator)) {
      datas.Add(i);
   }

   return datas.Count(d => d.DayOfWeek != DayOfWeek.Saturday && 
                           d.DayOfWeek != DayOfWeek.Sunday) * fator;            

}
    
14.06.2016 / 19:43