title: AddBusinessDays and GetBusinessDays
body: <p>I need to find 2 elegant complete implementations of </p>
  
  <pre><code>public static DateTime AddBusinessDays(this DateTime date, int days)
  {
   // code here
  }
  
  and 
  
  public static int GetBusinessDays(this DateTime start, DateTime end)
  {
   // code here
  }
  </code></pre>
  
  <p>O(1) preferable (no loops).</p>
  
  <p>EDIT:
   By business days i mean working days (Monday, Tuesday, Wednesday, Thursday, Friday). No holidays, just weekends excluded. </p>
  
  <p>I already have some ugly solutions that seem to work but i wonder if there are elegant ways to do this. Thanks</p>
  
  <hr>
  
  <p>This is what i've written so far. It works in all cases and does negatives too.
  Still need a GetBusinessDays implementation</p>
  
  <pre><code>public static DateTime AddBusinessDays(this DateTime startDate,
                                           int businessDays)
  {
      int direction = Math.Sign(businessDays);
      if(direction == 1)
      {
          if(startDate.DayOfWeek == DayOfWeek.Saturday)
          {
              startDate = startDate.AddDays(2);
              businessDays = businessDays - 1;
          }
          else if(startDate.DayOfWeek == DayOfWeek.Sunday)
          {
              startDate = startDate.AddDays(1);
              businessDays = businessDays - 1;
          }
      }
      else
      {
          if(startDate.DayOfWeek == DayOfWeek.Saturday)
          {
              startDate = startDate.AddDays(-1);
              businessDays = businessDays + 1;
          }
          else if(startDate.DayOfWeek == DayOfWeek.Sunday)
          {
              startDate = startDate.AddDays(-2);
              businessDays = businessDays + 1;
          }
      }
  
      int initialDayOfWeek = (int)startDate.DayOfWeek;
  
      int weeksBase = Math.Abs(businessDays / 5);
      int addDays = Math.Abs(businessDays % 5);
  
      if((direction == 1 &amp;&amp; addDays + initialDayOfWeek &gt; 5) ||
           (direction == -1 &amp;&amp; addDays &gt;= initialDayOfWeek))
      {
          addDays += 2;
      }
  
      int totalDays = (weeksBase * 7) + addDays;
      return startDate.AddDays(totalDays * direction);
  }
  </code></pre>