snipt

Ctrl+h for KB shortcuts

C#

Change the time of a DateTime

1
2
3
DateTime s = ...;
TimeSpan ts = new TimeSpan(10, 30, 0);
s = s.Date + ts;
https://snipt.net/embed/24b8fd009a48adaa0050a06412cad859/
/raw/24b8fd009a48adaa0050a06412cad859/
24b8fd009a48adaa0050a06412cad859
csharp
C#
3
2019-06-14T02:13:57
True
False
False
/api/public/snipt/38773/
change-the-time-of-a-datetime
<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></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">s</span> <span class="p">=</span> <span class="p">...;</span> </span><span id="L-2"><a name="L-2"></a><span class="n">TimeSpan</span> <span class="n">ts</span> <span class="p">=</span> <span class="k">new</span> <span class="n">TimeSpan</span><span class="p">(</span><span class="m">10</span><span class="p">,</span> <span class="m">30</span><span class="p">,</span> <span class="m">0</span><span class="p">);</span> </span><span id="L-3"><a name="L-3"></a><span class="n">s</span> <span class="p">=</span> <span class="n">s</span><span class="p">.</span><span class="n">Date</span> <span class="p">+</span> <span class="n">ts</span><span class="p">;</span> </span></pre></div> </td></tr></table>
datetime

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-06-10T02:28:57
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

C#

Get number of Days between two DateTime

1
2
3
DateTime d1;
DateTime d2;
double diff = (d1 - d2).TotalDays;
https://snipt.net/embed/ef8528afb70c446ac1c97c8bf83e985d/
/raw/ef8528afb70c446ac1c97c8bf83e985d/
ef8528afb70c446ac1c97c8bf83e985d
csharp
C#
3
2019-06-08T12:17:35
True
False
False
/api/public/snipt/37648/
get-number-of-days-between-two-datetime
<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></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">d1</span><span class="p">;</span> </span><span id="L-2"><a name="L-2"></a><span class="n">DateTime</span> <span class="n">d2</span><span class="p">;</span> </span><span id="L-3"><a name="L-3"></a><span class="kt">double</span> <span class="n">diff</span> <span class="p">=</span> <span class="p">(</span><span class="n">d1</span> <span class="p">-</span> <span class="n">d2</span><span class="p">).</span><span class="n">TotalDays</span><span class="p">;</span> </span></pre></div> </td></tr></table>
datetime