snipt

Ctrl+h for KB shortcuts

C#

Calculate differcence between UTC and Local time

1
2
3
4
5
6
DateTime localTime = System.DateTime.Now;
DateTime utcTime = System.DateTime.UtcNow;

TimeSpan timeDifference = localTime - utcTime;
 
int hourDifference = timeDifference.Hours;
https://snipt.net/embed/b837ed5c44185e2350cc790baf720e68/
/raw/b837ed5c44185e2350cc790baf720e68/
b837ed5c44185e2350cc790baf720e68
csharp
C#
6
2019-08-18T21:19:14
True
False
False
/api/public/snipt/38771/
calculate-differcence-between-utc-and-local-time
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a> <a href="#L-3">3</a> <a href="#L-4">4</a> <a href="#L-5">5</a> <a href="#L-6">6</a></pre></div></td><td class="code"><div class="highlight"><pre><span></span><span id="L-1"><a name="L-1"></a><span class="n">DateTime</span> <span class="n">localTime</span> <span class="p">=</span> <span class="n">System</span><span class="p">.</span><span class="n">DateTime</span><span class="p">.</span><span class="n">Now</span><span class="p">;</span> </span><span id="L-2"><a name="L-2"></a><span class="n">DateTime</span> <span class="n">utcTime</span> <span class="p">=</span> <span class="n">System</span><span class="p">.</span><span class="n">DateTime</span><span class="p">.</span><span class="n">UtcNow</span><span class="p">;</span> </span><span id="L-3"><a name="L-3"></a> </span><span id="L-4"><a name="L-4"></a><span class="n">TimeSpan</span> <span class="n">timeDifference</span> <span class="p">=</span> <span class="n">localTime</span> <span class="p">-</span> <span class="n">utcTime</span><span class="p">;</span> </span><span id="L-5"><a name="L-5"></a> </span><span id="L-6"><a name="L-6"></a><span class="kt">int</span> <span class="n">hourDifference</span> <span class="p">=</span> <span class="n">timeDifference</span><span class="p">.</span><span class="n">Hours</span><span class="p">;</span> </span></pre></div> </td></tr></table>
datetime