snipt

Ctrl+h for KB shortcuts

Bash

AWK timestamp each line

<cmd> | awk '{ print strftime("%Y-%m-%d %H:%M:%S"), $0; fflush(); }'
https://snipt.net/embed/3c95a0173f04f01070d3df97f8aa825b/
https://snipt.net/raw/3c95a0173f04f01070d3df97f8aa825b/
3c95a0173f04f01070d3df97f8aa825b
bash
Bash
1
2016-09-26T12:09:12
True
False
False
Mar 25, 2016 at 06:05 PM
/api/public/snipt/144950/
awk-timestamp-each-line
<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>&lt;cmd&gt; <span class="p">|</span> awk <span class="s1">&#39;{ print strftime(&quot;%Y-%m-%d %H:%M:%S&quot;), $0; fflush(); }&#39;</span> </span></pre></div> </td></tr></table>
awk, bash, console, timestamp

PHP

Timestamp in PHP for up to the minute changes

<!-- Just because I'm bored to look at the PHP docs -->
<?php echo date('YmdHi'); ?>
https://snipt.net/embed/b6f70ec894c0566d2eb5cf9b6ff37027/
https://snipt.net/raw/b6f70ec894c0566d2eb5cf9b6ff37027/
b6f70ec894c0566d2eb5cf9b6ff37027
php
PHP
3
2016-09-26T02:57:05
True
False
False
Feb 08, 2014 at 07:45 AM
/api/public/snipt/111918/
timestamp-in-php-for-up-to-the-minute-changes
<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="x">&lt;!-- Just because I&#39;m bored to look at the PHP docs --&gt;</span> </span><span id="L-2"><a name="L-2"></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;YmdHi&#39;</span><span class="p">);</span> <span class="cp">?&gt;</span><span class="x"></span> </span></pre></div> </td></tr></table>
php, timestamp

Objective-C

IRFC 3339 timestamp format yyyy-MM-dd'T'HH:mm:ss.SSS'Z' to NSDate

1
2
3
4
5
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"UTC"]];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"];
NSDate *date = [formatter dateFromString:@"2011-01-12T14:17:55.043Z"];
NSLog(@"Date: %@", date);
https://snipt.net/embed/bf54e9d6209e6eb5523d4a476e80b56b/
https://snipt.net/raw/bf54e9d6209e6eb5523d4a476e80b56b/
bf54e9d6209e6eb5523d4a476e80b56b
objective-c
Objective-C
5
2016-09-26T10:29:53
True
False
False
/api/public/snipt/76092/
-8052
<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></pre></div></td><td class="code"><div class="highlight"><pre><span id="L-1"><a name="L-1"></a><span class="bp">NSDateFormatter</span> <span class="o">*</span><span class="n">formatter</span> <span class="o">=</span> <span class="p">[[</span><span class="bp">NSDateFormatter</span> <span class="n">alloc</span><span class="p">]</span> <span class="n">init</span><span class="p">];</span> </span><span id="L-2"><a name="L-2"></a><span class="p">[</span><span class="n">formatter</span> <span class="nl">setTimeZone</span><span class="p">:[</span><span class="bp">NSTimeZone</span> <span class="nl">timeZoneWithName</span><span class="p">:</span><span class="s">@&quot;UTC&quot;</span><span class="p">]];</span> </span><span id="L-3"><a name="L-3"></a><span class="p">[</span><span class="n">formatter</span> <span class="nl">setDateFormat</span><span class="p">:</span><span class="s">@&quot;yyyy-MM-dd&#39;T&#39;HH:mm:ss.SSS&#39;Z&#39;&quot;</span><span class="p">];</span> </span><span id="L-4"><a name="L-4"></a><span class="bp">NSDate</span> <span class="o">*</span><span class="n">date</span> <span class="o">=</span> <span class="p">[</span><span class="n">formatter</span> <span class="nl">dateFromString</span><span class="p">:</span><span class="s">@&quot;2011-01-12T14:17:55.043Z&quot;</span><span class="p">];</span> </span><span id="L-5"><a name="L-5"></a><span class="n">NSLog</span><span class="p">(</span><span class="s">@&quot;Date: %@&quot;</span><span class="p">,</span> <span class="n">date</span><span class="p">);</span> </span></pre></div> </td></tr></table>
"IRFC 3339", NSDate, timestamp, yyyy-MM-dd'T'HH:mm:ss.SSS'Z'

PHP

PHP: Get timestamp First day of the month (or last)

1
2
3
4
<?php

$tsFirstDayOfMonth = strtotime('first day of '.date('F Y'));
$tsLastDayOfMonth  = strtotime('last day of '.date('F Y'));
https://snipt.net/embed/fd5716b81a211fef6b91201bf874aa94/
https://snipt.net/raw/fd5716b81a211fef6b91201bf874aa94/
fd5716b81a211fef6b91201bf874aa94
php
PHP
4
2016-09-26T05:01:48
True
False
True
Nov 19, 2012 at 10:38 AM
/api/public/snipt/50461/
php-get-timestamp-first-day-of-the-month-or-last
<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></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><span id="L-3"><a name="L-3"></a><span class="nv">$tsFirstDayOfMonth</span> <span class="o">=</span> <span class="nb">strtotime</span><span class="p">(</span><span class="s1">&#39;first day of &#39;</span><span class="o">.</span><span class="nb">date</span><span class="p">(</span><span class="s1">&#39;F Y&#39;</span><span class="p">));</span> </span><span id="L-4"><a name="L-4"></a><span class="nv">$tsLastDayOfMonth</span> <span class="o">=</span> <span class="nb">strtotime</span><span class="p">(</span><span class="s1">&#39;last day of &#39;</span><span class="o">.</span><span class="nb">date</span><span class="p">(</span><span class="s1">&#39;F Y&#39;</span><span class="p">));</span> </span></pre></div> </td></tr></table>
date, php, timestamp

JavaScript

Get Timestamp and Date from Mongo ID

var timestamp = _id.toString().substring( 0, 8 );
var date = new Date( parseInt( timestamp, 16 ) * 1000 );
https://snipt.net/embed/dc5fb59a142040acdfe7da784540b72f/
https://snipt.net/raw/dc5fb59a142040acdfe7da784540b72f/
dc5fb59a142040acdfe7da784540b72f
js
JavaScript
2
2016-09-26T12:23:19
True
False
False
Oct 02, 2012 at 11:40 AM
/api/public/snipt/47925/
get-timestamp-and-date-from-mongo-id
<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="kd">var</span> <span class="nx">timestamp</span> <span class="o">=</span> <span class="nx">_id</span><span class="p">.</span><span class="nx">toString</span><span class="p">().</span><span class="nx">substring</span><span class="p">(</span> <span class="mi">0</span><span class="p">,</span> <span class="mi">8</span> <span class="p">);</span> </span><span id="L-2"><a name="L-2"></a><span class="kd">var</span> <span class="nx">date</span> <span class="o">=</span> <span class="k">new</span> <span class="nb">Date</span><span class="p">(</span> <span class="nb">parseInt</span><span class="p">(</span> <span class="nx">timestamp</span><span class="p">,</span> <span class="mi">16</span> <span class="p">)</span> <span class="o">*</span> <span class="mi">1000</span> <span class="p">);</span> </span></pre></div> </td></tr></table>
date, id, mongo, timestamp

JavaScript

JS convert date to unixtimestamp

1
2
3
4
5
6
//Convert JS date to unixtimestamp

Math.round(date.getTime() / 1000);

//or current date
Math.round(new Date().getTime() / 1000);
https://snipt.net/embed/4ccbcc89c05629ad464d321ab4380967/
https://snipt.net/raw/4ccbcc89c05629ad464d321ab4380967/
4ccbcc89c05629ad464d321ab4380967
js
JavaScript
6
2016-09-25T06:41:35
True
False
False
/api/public/snipt/20853/
js-convert-date-to-unixtimestamp
<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="c1">//Convert JS date to unixtimestamp</span> </span><span id="L-2"><a name="L-2"></a> </span><span id="L-3"><a name="L-3"></a><span class="nb">Math</span><span class="p">.</span><span class="nx">round</span><span class="p">(</span><span class="nx">date</span><span class="p">.</span><span class="nx">getTime</span><span class="p">()</span> <span class="o">/</span> <span class="mi">1000</span><span class="p">);</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="c1">//or current date</span> </span><span id="L-6"><a name="L-6"></a><span class="nb">Math</span><span class="p">.</span><span class="nx">round</span><span class="p">(</span><span class="k">new</span> <span class="nb">Date</span><span class="p">().</span><span class="nx">getTime</span><span class="p">()</span> <span class="o">/</span> <span class="mi">1000</span><span class="p">);</span> </span></pre></div> </td></tr></table>
date, javascript, js, timestamp, unix

Bash

bash timestamp variable

$ timestamp=`date +%Y-%m-%d`
https://snipt.net/embed/eb10fc5079f4c1c18ce8a178759f8f36/
https://snipt.net/raw/eb10fc5079f4c1c18ce8a178759f8f36/
eb10fc5079f4c1c18ce8a178759f8f36
bash
Bash
1
2016-09-21T13:18:46
True
False
False
/api/public/snipt/20779/
bash-timestamp-variable
<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="nv">$ timestamp</span><span class="o">=</span><span class="sb">`</span>date +%Y-%m-%d<span class="sb">`</span> </span></pre></div> </td></tr></table>
backup, logs, script, time, timestamp

C#

Timestamp to/from string conversion

1
2
3
4
5
6
// to string
// cast to system binary if not using linq
Convert.ToBase64String(((System.Data.Linq.Binary)myTimestamp).ToArray())

// to binary
Convert.FromBase64String(myString);
https://snipt.net/embed/7afc9e8023e8596bf2f46ef461540b1b/
https://snipt.net/raw/7afc9e8023e8596bf2f46ef461540b1b/
7afc9e8023e8596bf2f46ef461540b1b
csharp
C#
6
2016-09-26T11:20:15
True
False
False
/api/public/snipt/8139/
timestamp-tofrom-string-conversion
<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="c1">// to string</span> </span><span id="L-2"><a name="L-2"></a><span class="c1">// cast to system binary if not using linq</span> </span><span id="L-3"><a name="L-3"></a><span class="n">Convert</span><span class="p">.</span><span class="n">ToBase64String</span><span class="p">(((</span><span class="n">System</span><span class="p">.</span><span class="n">Data</span><span class="p">.</span><span class="n">Linq</span><span class="p">.</span><span class="n">Binary</span><span class="p">)</span><span class="n">myTimestamp</span><span class="p">).</span><span class="n">ToArray</span><span class="p">())</span> </span><span id="L-4"><a name="L-4"></a> </span><span id="L-5"><a name="L-5"></a><span class="c1">// to binary</span> </span><span id="L-6"><a name="L-6"></a><span class="n">Convert</span><span class="p">.</span><span class="n">FromBase64String</span><span class="p">(</span><span class="n">myString</span><span class="p">);</span> </span></pre></div> </td></tr></table>
binary, timestamp
Copyrighted, illegal, or inappropriate content? Email support@snipt.net.