snipt

Ctrl+h for KB shortcuts
#113504

SQL

Casting a Datetime to Date Sqlserver 2008

cast(getdate() As Date)
https://snipt.net/embed/842fae8370d256c3ff55a2a5a1656f88/
https://snipt.net/raw/842fae8370d256c3ff55a2a5a1656f88/
842fae8370d256c3ff55a2a5a1656f88
sql
SQL
1
2014-04-16T23:07:29
True
False
Feb 13, 2014 at 02:52 PM
/api/public/snipt/113504/
casting-a-datetime-to-date-sqlserver-2008
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="k">cast</span><span class="p">(</span><span class="n">getdate</span><span class="p">()</span> <span class="k">As</span> <span class="nb">Date</span><span class="p">)</span> </span></pre></div> </td></tr></table>
casting, date, datetime, sqlserver
#61357

PHP

PHP DateTime Class timezone and formatting

1
2
3
4
5
6
7
<?php 
    // Convert to UTC
    $date = new DateTime($pageDate);
    $tz = $date->getTimezone();
    $date->setTimezone(new DateTimeZone('UTC'));
    $arr['date']= $date->format('Y-m-d H:i');
?>	
https://snipt.net/embed/72e626b1904c45624ce677479b76155c/
https://snipt.net/raw/72e626b1904c45624ce677479b76155c/
72e626b1904c45624ce677479b76155c
php
PHP
7
2014-04-03T10:23:21
True
False
May 14, 2013 at 03:40 PM
/api/public/snipt/61357/
php-datetime-class-timezone-and-formatting
<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> <a href="#L-7">7</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> </span><span id="L-2"><a name="L-2"></a> <span class="c1">// Convert to UTC</span> </span><span id="L-3"><a name="L-3"></a> <span class="nv">$date</span> <span class="o">=</span> <span class="k">new</span> <span class="nx">DateTime</span><span class="p">(</span><span class="nv">$pageDate</span><span class="p">);</span> </span><span id="L-4"><a name="L-4"></a> <span class="nv">$tz</span> <span class="o">=</span> <span class="nv">$date</span><span class="o">-&gt;</span><span class="na">getTimezone</span><span class="p">();</span> </span><span id="L-5"><a name="L-5"></a> <span class="nv">$date</span><span class="o">-&gt;</span><span class="na">setTimezone</span><span class="p">(</span><span class="k">new</span> <span class="nx">DateTimeZone</span><span class="p">(</span><span class="s1">&#39;UTC&#39;</span><span class="p">));</span> </span><span id="L-6"><a name="L-6"></a> <span class="nv">$arr</span><span class="p">[</span><span class="s1">&#39;date&#39;</span><span class="p">]</span><span class="o">=</span> <span class="nv">$date</span><span class="o">-&gt;</span><span class="na">format</span><span class="p">(</span><span class="s1">&#39;Y-m-d H:i&#39;</span><span class="p">);</span> </span><span id="L-7"><a name="L-7"></a><span class="cp">?&gt;</span><span class="x"> </span> </span></pre></div> </td></tr></table>
datetime, php
#55633

PHP

PHP year

<?php echo date('Y') ?>
https://snipt.net/embed/6a1ce5f71cd4659be34fd61af15bb09b/
https://snipt.net/raw/6a1ce5f71cd4659be34fd61af15bb09b/
6a1ce5f71cd4659be34fd61af15bb09b
php
PHP
1
2014-04-17T12:56:04
True
False
Feb 12, 2013 at 11:21 AM
/api/public/snipt/55633/
php-year-1
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="cp">&lt;?php</span> <span class="k">echo</span> <span class="nb">date</span><span class="p">(</span><span class="s1">&#39;Y&#39;</span><span class="p">)</span> <span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
datetime, php
#47452

Java

Get current datetime in SQL timestamp format

java.util.Date date = new java.util.Date();
Timestamp timestamp = new Timestamp(date.getTime());
https://snipt.net/embed/a762824f297079ef9fd67422bbb04159/
https://snipt.net/raw/a762824f297079ef9fd67422bbb04159/
a762824f297079ef9fd67422bbb04159
java
Java
2
2014-04-13T02:05:08
True
False
Sep 21, 2012 at 08:25 AM
/api/public/snipt/47452/
get-current-datetime-in-sql-timestamp-format
<table class="highlighttable"><tr><td class="linenos"><div class="linenodiv"><pre><a href="#L-1">1</a> <a href="#L-2">2</a></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="n">java</span><span class="o">.</span><span class="na">util</span><span class="o">.</span><span class="na">Date</span> <span class="n">date</span> <span class="o">=</span> <span class="k">new</span> <span class="n">java</span><span class="o">.</span><span class="na">util</span><span class="o">.</span><span class="na">Date</span><span class="o">();</span> </span><span id="L-2"><a name="L-2"></a><span class="n">Timestamp</span> <span class="n">timestamp</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Timestamp</span><span class="o">(</span><span class="n">date</span><span class="o">.</span><span class="na">getTime</span><span class="o">());</span> </span></pre></div> </td></tr></table>
datetime, sql
#44278

Ruby

Convert to local datetime in Ruby (and Rails)

1
2
3
#Assuming a local active record model named Lead...
lead.created_at.strftime("%m/%d/%y %l:%M %p")
#outputs this as 07/07/12  6:04 PM
https://snipt.net/embed/41ff922a9adb08fe295ac63eceb8516f/
https://snipt.net/raw/41ff922a9adb08fe295ac63eceb8516f/
41ff922a9adb08fe295ac63eceb8516f
rb
Ruby
3
2014-04-16T12:06:53
True
False
Jul 21, 2012 at 10:34 PM
/api/public/snipt/44278/
convert-to-local-datetime-in-ruby-and-rails
<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 id="L-1"><a name="L-1"></a><span class="c1">#Assuming a local active record model named Lead...</span> </span><span id="L-2"><a name="L-2"></a><span class="n">lead</span><span class="o">.</span><span class="n">created_at</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s2">&quot;%m/%d/%y %l:%M %p&quot;</span><span class="p">)</span> </span><span id="L-3"><a name="L-3"></a><span class="c1">#outputs this as 07/07/12 6:04 PM</span> </span></pre></div> </td></tr></table>
date_format, datetime, rails, ruby
#38773

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/
https://snipt.net/raw/24b8fd009a48adaa0050a06412cad859/
24b8fd009a48adaa0050a06412cad859
csharp
C#
3
2014-04-16T06:28:04
True
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 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
#38771

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/
https://snipt.net/raw/b837ed5c44185e2350cc790baf720e68/
b837ed5c44185e2350cc790baf720e68
csharp
C#
6
2014-04-17T11:39:44
True
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 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
#37648

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/
https://snipt.net/raw/ef8528afb70c446ac1c97c8bf83e985d/
ef8528afb70c446ac1c97c8bf83e985d
csharp
C#
3
2014-04-16T15:52:14
True
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 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