title: How can I calculate the acutal elapsed time in my timezone between two dates in C#?
body: <p>How do I calculate the <strong>actual elapsed</strong> time between two <code>DateTime</code>/<code>DateTimeOffset</code>, both in my timezone, taking daylight saving time, leap years, etc, into account?</p>
  <p>For example, the following code comparing the difference between 00:00 and 05:00 of March 29th 2020 will output a timespan of 6 hours (05:00 becomes 06:00 after the conversion to local time, since daylight saving time starts 02:00 in my timezone, changing clocks forward from 02:00 to 03:00).</p>
  <pre class="lang-cs prettyprint-override"><code>var timezone = TimeZoneInfo.Local;
  
  var start = new DateTimeOffset(2020, 03, 29, 00, 00, 00, timezone.BaseUtcOffset).LocalDateTime;
  var end   = new DateTimeOffset(2020, 03, 29, 05, 00, 00, timezone.BaseUtcOffset).LocalDateTime;
  
  var diff1 = end.Subtract(start);
  var diff2 = end - start;
  
  Console.WriteLine(diff1); // 06:00:00
  Console.WriteLine(diff2); // 06:00:00
  </code></pre>
  <p>I'm trying to get an output of <strong>4 hours</strong> in the example above since that is the elapsed time between 00:00 and 05:00.</p>
  <pre><code>00:00 -&gt; 01:00 (+ 1 hr)
  01:00 -&gt; 02:00 (+ 1 hr)
  02:00 -&gt; 03:00 (0)
  03:00 -&gt; 04:00 (+ 1 hr)
  04:00 -&gt; 05:00 (+ 1 hr)
  
  = 4 hrs
  </code></pre>
  <p>I'm clearly missing something here. I've read <a href="https://docs.microsoft.com/en-us/dotnet/standard/datetime/performing-arithmetic-operations#comparisons-and-arithmetic-operations-with-datetimeoffset-values" rel="nofollow noreferrer">Comparisons and arithmetic operations with DateTimeOffset values</a> but can't wrap my head around how to do this calculation.</p>
  <p>I am aware that NodaTime can probably achieve this, but surely there's a way to do this without external libraries?</p>