Tutorial :Is there a .NET method equivalent to NETWORKDAYS in Excel? Question:

I'm trying to do some date math in Visual Basic. The `NETWORKDAYS` function in Excel is exactly what I need. Is there an equivalent or similar method available in .NET?

Solution:1

Take a look at this:

``private int CountWorkDays( DateTime startDate, DateTime endDate, List<DateTime> excludedDates )  {      int dayCount = 0;      int inc = 1;      bool endDateIsInPast = startDate > endDate;      DateTime tmpDate = startDate;      DateTime finiDate = endDate;        if( endDateIsInPast )      {          // Swap dates around          tmpDate = endDate;          finiDate = startDate;            // Set increment value to -1, so it DayCount decrements rather           // than increments          inc = -1;      }        while( tmpDate <= finiDate )      {          if( !excludedDates.Contains( tmpDate ) )          {              dayCount += inc;          }            // Move onto next day          tmpDate = tmpDate.AddDays( 1 );      }        return dayCount;  }  ``

Solution:2

Here you will have two articles as a good starting point:

Optimized Calculation Algorithm for Business Days

and