Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate the number of weekdays between two dates in C#

Tags:

How can I get the number of weekdays between two given dates without just iterating through the dates between and counting the weekdays?

Seems fairly straightforward but I can't seem to find a conclusive correct answer that abides by the following:

  1. The total should be inclusive, so GetNumberOfWeekdays(new DateTime(2009,11,30), new DateTime(2009,12,4)) should equal 5, that's Monday to Friday.
  2. Should allow for leap days
  3. does NOT just iterate through all the dates between whilst counting the weekdays.

I've found a similar question with an answer that comes close but is not correct

like image 345
David Glenn Avatar asked Nov 30 '09 14:11

David Glenn


People also ask

How do you count the number of weekdays between two dates?

First you get the amount of days between the two dates, and add one to that so that you can count the starting day. Then you calculate the amount of weekend days.

How do you calculate the number of Sundays between two dates?

Select a blank cell, here is C2, and type this formula =B2-A2-INT((B2-A2-WEEKDAY(B2)+1)/7) into it, and then press Enter key, a date displayed. Then keep the result selected, click Home tab, and go to the Number group to format the cell to General or Number in the Number Format drop-down list.


2 Answers

O(1) solution:

// Count days from d0 to d1 inclusive, excluding weekends public static int countWeekDays(DateTime d0, DateTime d1) {     int ndays = 1 + Convert.ToInt32((d1 - d0).TotalDays);     int nsaturdays = (ndays + Convert.ToInt32(d0.DayOfWeek)) / 7;     return ndays - 2 * nsaturdays            - (d0.DayOfWeek == DayOfWeek.Sunday ? 1 : 0)            + (d1.DayOfWeek == DayOfWeek.Saturday ? 1 : 0); } 

Examples for January 2014:

    January 2014 Su Mo Tu We Th Fr Sa           1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31  countWeekDays(new DateTime(2014, 1, 1), new DateTime(2014, 1, 1)); // 1 countWeekDays(new DateTime(2014, 1, 1), new DateTime(2014, 1, 2)); // 2 countWeekDays(new DateTime(2014, 1, 1), new DateTime(2014, 1, 3)); // 3 countWeekDays(new DateTime(2014, 1, 1), new DateTime(2014, 1, 4)); // 3 countWeekDays(new DateTime(2014, 1, 1), new DateTime(2014, 1, 5)); // 3 countWeekDays(new DateTime(2014, 1, 1), new DateTime(2014, 1, 6)); // 4 

N.B. The DateTime inputs should be at around the same time of the day. If you are creating DateTime objects based solely on year, month, and day as in the examples above, then you should be fine. As a counter example, 12:01am on Jan 1 to 11:59pm Jan 2 spans only 2 days, but the above function will count 3 if you use those times.

like image 128
Matt Avatar answered Sep 22 '22 20:09

Matt


From this link:

    public static int Weekdays(DateTime dtmStart, DateTime dtmEnd)     {         // This function includes the start and end date in the count if they fall on a weekday         int dowStart = ((int)dtmStart.DayOfWeek == 0 ? 7 : (int)dtmStart.DayOfWeek);         int dowEnd = ((int)dtmEnd.DayOfWeek == 0 ? 7 : (int)dtmEnd.DayOfWeek);         TimeSpan tSpan = dtmEnd - dtmStart;         if (dowStart <= dowEnd)         {             return (((tSpan.Days / 7) * 5) + Math.Max((Math.Min((dowEnd + 1), 6) - dowStart), 0));         }         return (((tSpan.Days / 7) * 5) + Math.Min((dowEnd + 6) - Math.Min(dowStart, 6), 5));     }     [1]: http://www.eggheadcafe.com/community/aspnet/2/44982/how-to-calculate-num-of-w.aspx 

Tests (each test returns 5):

    int ndays = Weekdays(new DateTime(2009, 11, 30), new DateTime(2009, 12, 4));     System.Console.WriteLine(ndays);      // leap year test     ndays = Weekdays(new DateTime(2000, 2,27), new DateTime(2000, 3, 5));     System.Console.WriteLine(ndays);      // non leap year test     ndays = Weekdays(new DateTime(2007, 2, 25), new DateTime(2007, 3, 4));     System.Console.WriteLine(ndays); 
like image 24
dcp Avatar answered Sep 24 '22 20:09

dcp