title: c-sharp Get working hours within a time span
body: <p>Given multiple time span, how can I get an array that gives me number of working hours for a day.</p>
  <p>The working hours start at 09:00 and end at 17:00</p>
  <p>Examples:</p>
  <ol>
  <li><p>15-Nov-2017 16:00 - 17-Nov-2017 12:00 -&gt; Should give me an array of [1,8,3]</p>
  </li>
  <li><p>15-Nov-2017 09:00 - 17-Nov-2017 12:00 -&gt; Should give me an array of [8,8,1] (This seems easy as I can do Hrs / 8 and Hrs % 8 to get the result.)</p>
  </li>
  <li><p>16-Nov-2017 15:00 - 17-Nov-2017 15:00 -&gt; Should give me an array of [2,6]</p>
  </li>
  </ol>
  <p>This is the method that currently gives me total working hours between the start time and end time:</p>
  <pre><code>protected double getWorkingHrs(DateTime _dtTaskStart, DateTime _dtTaskEnd)
  {
      double count = 0;
  
      for (var i = _dtTaskStart; i &lt; _dtTaskEnd; i = i.AddHours(1))
      {
          if (i.DayOfWeek != DayOfWeek.Saturday &amp;&amp; i.DayOfWeek != DayOfWeek.Sunday)
          {
              if (i.TimeOfDay.Hours &gt;= 9 &amp;&amp; i.TimeOfDay.Hours &lt; 17)
              {
                  count++;
              }
          }
      }
  
      return count;
  }
  </code></pre>
  <p>Any help would be appreciated.</p>
  <p>Task showed like this in 8 hour blocks. So when I add a 20 Hour task, I have to break them down into sets and display them in the grid. I have to split them as chances are the task on different day might get assigned to a different user and may have different notes etc.</p>
  <p><a href="https://i.stack.imgur.com/YK9ZN.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/YK9ZN.png" alt="UI for placing tasks" /></a></p>
  <p>Cheers</p>