Skip to content

Commit bceeea4

Browse files
added examples for date arithmetic
1 parent a466136 commit bceeea4

File tree

3 files changed

+25
-4
lines changed

3 files changed

+25
-4
lines changed

atom.xml

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
<link href="https://learnbyexample.github.io/atom.xml" rel="self" type="application/atom+xml"/>
66
<link href="https://learnbyexample.github.io"/>
77
<generator uri="https://www.getzola.org/">Zola</generator>
8-
<updated>2024-07-31T00:00:00+00:00</updated>
8+
<updated>2024-08-02T00:00:00+00:00</updated>
99
<id>https://learnbyexample.github.io/atom.xml</id>
1010
<entry xml:lang="en">
1111
<title>Basic examples for the Linux date command</title>
1212
<published>2024-07-31T00:00:00+00:00</published>
13-
<updated>2024-07-31T00:00:00+00:00</updated>
13+
<updated>2024-08-02T00:00:00+00:00</updated>
1414
<link rel="alternate" href="https://learnbyexample.github.io/mini/linux-date-command-examples/" type="text/html"/>
1515
<id>https://learnbyexample.github.io/mini/linux-date-command-examples/</id>
1616
<content type="html">&lt;p&gt;I rarely ever use the &lt;code&gt;date&lt;&#x2F;code&gt; command, but when I need it I almost always struggle to get the right incantation. So, I&#x27;m just going to record such examples in this blog post (and some good to know features).&lt;&#x2F;p&gt;
@@ -61,6 +61,18 @@
6161
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;2024&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;07&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;31 16&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;09&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;53
6262
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
6363
&lt;br&gt;
64+
&lt;h2 id=&quot;date-arithmetic&quot;&gt;Date arithmetic&lt;&#x2F;h2&gt;
65+
&lt;pre data-lang=&quot;ruby&quot; style=&quot;background-color:#f5f5f5;color:#1f1f1f;&quot; class=&quot;language-ruby &quot;&gt;&lt;code class=&quot;language-ruby&quot; data-lang=&quot;ruby&quot;&gt;&lt;span&gt;$ date &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;I
66+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;2024&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;08&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;02
67+
&lt;&#x2F;span&gt;&lt;span&gt;$ date &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;d &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;+1 month 4 days&amp;#39;
68+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;Friday &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;06 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;September &lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;2024 01&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;24&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;:&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;44 &lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;PM IST
69+
&lt;&#x2F;span&gt;&lt;span&gt;
70+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# same as: date -d &amp;#39;-20 days&amp;#39; +%F
71+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#7f8989;&quot;&gt;# you can also use &amp;#39;20 days ago&amp;#39;
72+
&lt;&#x2F;span&gt;&lt;span&gt;$ date &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#5597d6;&quot;&gt;I &lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span&gt;d &lt;&#x2F;span&gt;&lt;span style=&quot;color:#d07711;&quot;&gt;&amp;#39;-20 days&amp;#39;
73+
&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;2024&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;07&lt;&#x2F;span&gt;&lt;span style=&quot;color:#72ab00;&quot;&gt;-&lt;&#x2F;span&gt;&lt;span style=&quot;color:#b3933a;&quot;&gt;13
74+
&lt;&#x2F;span&gt;&lt;&#x2F;code&gt;&lt;&#x2F;pre&gt;
75+
&lt;br&gt;
6476
&lt;h2 id=&quot;resource-links&quot;&gt;Resource links&lt;&#x2F;h2&gt;
6577
&lt;ul&gt;
6678
&lt;li&gt;&lt;a href=&quot;https:&#x2F;&#x2F;www.gnu.org&#x2F;software&#x2F;coreutils&#x2F;manual&#x2F;coreutils.html#date-invocation&quot;&gt;GNU date command manual&lt;&#x2F;a&gt;&lt;&#x2F;li&gt;

mini/linux-date-command-examples/index.html

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!doctype html><html lang=en><head><meta content="IE=edge" http-equiv=X-UA-Compatible><meta content="text/html; charset=utf-8" http-equiv=content-type><meta content="width=device-width,initial-scale=1.0,maximum-scale=1" name=viewport><title>Basic examples for the Linux date command</title><link href=https://learnbyexample.github.io/atom.xml rel=alternate title=RSS type=application/atom+xml><script src=https://cdnjs.cloudflare.com/ajax/libs/slideout/1.0.1/slideout.min.js></script><link href=https://learnbyexample.github.io/site.css rel=stylesheet><meta content="Basic examples for the Linux date command" property=og:title><meta content=website property=og:type><meta content="Examples and resource links for the Linux date command." property=og:description><meta content=https://learnbyexample.github.io/linux-date-command-examples/ property=og:url><meta content=@learn_byexample property=twitter:site><link href=https://learnbyexample.github.io/favicon.svg rel=icon><link rel="shortcut icon" href=https://learnbyexample.github.io/favicon.png><body><div class=container><div class=mobile-navbar id=mobile-navbar><div class=mobile-header-logo><a class=logo href=/>learnbyexample</a></div><div class="mobile-navbar-icon icon-out"><span></span><span></span><span></span></div></div><nav class="mobile-menu slideout-menu slideout-menu-left" id=mobile-menu><ul class=mobile-menu-list><li class=mobile-menu-item><a href=https://learnbyexample.github.io/books> Books </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/mini> Mini </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/tips> Tips </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/tags> Tags </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/about> About </a></ul></nav><header id=header><div class=logo><a href=https://learnbyexample.github.io>learnbyexample</a></div><nav class=menu><ul><li><a href=https://learnbyexample.github.io/books> Books </a><li><a href=https://learnbyexample.github.io/mini> Mini </a><li><a href=https://learnbyexample.github.io/tips> Tips </a><li><a href=https://learnbyexample.github.io/tags> Tags </a><li><a href=https://learnbyexample.github.io/about> About </a></ul></nav></header><main><div class=content id=mobile-panel><div class=post-toc id=post-toc><h2 class=post-toc-title>Contents</h2><div class="post-toc-content always-active"><nav id=TableOfContents><ul><li><a class=toc-link href=https://learnbyexample.github.io/mini/linux-date-command-examples/#really-basic-examples>Really basic examples</a><li><a class=toc-link href=https://learnbyexample.github.io/mini/linux-date-command-examples/#displaying-and-converting-epoch-seconds>Displaying and converting epoch seconds</a><li><a class=toc-link href=https://learnbyexample.github.io/mini/linux-date-command-examples/#resource-links>Resource links</a></ul></nav></div></div><article class=post><header class=post__header><h1 class=post__title><a href=https://learnbyexample.github.io/mini/linux-date-command-examples/>Basic examples for the Linux date command</a></h1><div class=post__meta><span class=post__time>2024-07-31</span></div></header><div class=post-content><p>I rarely ever use the <code>date</code> command, but when I need it I almost always struggle to get the right incantation. So, I'm just going to record such examples in this blog post (and some good to know features).<p>There'll also be learning resources linked at the end of the post.</p><br><h2 id=really-basic-examples>Really basic examples</h2><p>The <code>date</code> command by itself shows the current time. But that's rarely what I need, since I could just use the calendar widget at the bottom of my desktop screen. Perhaps useful to copy the string format and modify system time with the <code>-s</code> option?<pre class=language-ruby data-lang=ruby style=background-color:#f5f5f5;color:#1f1f1f;><code class=language-ruby data-lang=ruby><span style=color:#7f8989;># use the -u option for UTC (Coordinated Universal Time)
1+
<!doctype html><html lang=en><head><meta content="IE=edge" http-equiv=X-UA-Compatible><meta content="text/html; charset=utf-8" http-equiv=content-type><meta content="width=device-width,initial-scale=1.0,maximum-scale=1" name=viewport><title>Basic examples for the Linux date command</title><link href=https://learnbyexample.github.io/atom.xml rel=alternate title=RSS type=application/atom+xml><script src=https://cdnjs.cloudflare.com/ajax/libs/slideout/1.0.1/slideout.min.js></script><link href=https://learnbyexample.github.io/site.css rel=stylesheet><meta content="Basic examples for the Linux date command" property=og:title><meta content=website property=og:type><meta content="Examples and resource links for the Linux date command." property=og:description><meta content=https://learnbyexample.github.io/linux-date-command-examples/ property=og:url><meta content=@learn_byexample property=twitter:site><link href=https://learnbyexample.github.io/favicon.svg rel=icon><link rel="shortcut icon" href=https://learnbyexample.github.io/favicon.png><body><div class=container><div class=mobile-navbar id=mobile-navbar><div class=mobile-header-logo><a class=logo href=/>learnbyexample</a></div><div class="mobile-navbar-icon icon-out"><span></span><span></span><span></span></div></div><nav class="mobile-menu slideout-menu slideout-menu-left" id=mobile-menu><ul class=mobile-menu-list><li class=mobile-menu-item><a href=https://learnbyexample.github.io/books> Books </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/mini> Mini </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/tips> Tips </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/tags> Tags </a><li class=mobile-menu-item><a href=https://learnbyexample.github.io/about> About </a></ul></nav><header id=header><div class=logo><a href=https://learnbyexample.github.io>learnbyexample</a></div><nav class=menu><ul><li><a href=https://learnbyexample.github.io/books> Books </a><li><a href=https://learnbyexample.github.io/mini> Mini </a><li><a href=https://learnbyexample.github.io/tips> Tips </a><li><a href=https://learnbyexample.github.io/tags> Tags </a><li><a href=https://learnbyexample.github.io/about> About </a></ul></nav></header><main><div class=content id=mobile-panel><div class=post-toc id=post-toc><h2 class=post-toc-title>Contents</h2><div class="post-toc-content always-active"><nav id=TableOfContents><ul><li><a class=toc-link href=https://learnbyexample.github.io/mini/linux-date-command-examples/#really-basic-examples>Really basic examples</a><li><a class=toc-link href=https://learnbyexample.github.io/mini/linux-date-command-examples/#displaying-and-converting-epoch-seconds>Displaying and converting epoch seconds</a><li><a class=toc-link href=https://learnbyexample.github.io/mini/linux-date-command-examples/#date-arithmetic>Date arithmetic</a><li><a class=toc-link href=https://learnbyexample.github.io/mini/linux-date-command-examples/#resource-links>Resource links</a></ul></nav></div></div><article class=post><header class=post__header><h1 class=post__title><a href=https://learnbyexample.github.io/mini/linux-date-command-examples/>Basic examples for the Linux date command</a></h1><div class=post__meta><span class=post__time>2024-07-31</span></div></header><div class=post-content><p>I rarely ever use the <code>date</code> command, but when I need it I almost always struggle to get the right incantation. So, I'm just going to record such examples in this blog post (and some good to know features).<p>There'll also be learning resources linked at the end of the post.</p><br><h2 id=really-basic-examples>Really basic examples</h2><p>The <code>date</code> command by itself shows the current time. But that's rarely what I need, since I could just use the calendar widget at the bottom of my desktop screen. Perhaps useful to copy the string format and modify system time with the <code>-s</code> option?<pre class=language-ruby data-lang=ruby style=background-color:#f5f5f5;color:#1f1f1f;><code class=language-ruby data-lang=ruby><span style=color:#7f8989;># use the -u option for UTC (Coordinated Universal Time)
22
</span><span>$ date
33
</span><span style=color:#5597d6;>Wednesday </span><span style=color:#b3933a;>31 </span><span style=color:#5597d6;>July </span><span style=color:#b3933a;>2024 03</span><span style=color:#72ab00;>:</span><span style=color:#b3933a;>53</span><span style=color:#72ab00;>:</span><span style=color:#b3933a;>01 </span><span style=color:#5597d6;>PM IST
44
</span></code></pre><p>Instead, I need particular parts in a particular format. For example, to represent the time component in a dynamically constructed filename as part of a shell script.<pre class=language-ruby data-lang=ruby style=background-color:#f5f5f5;color:#1f1f1f;><code class=language-ruby data-lang=ruby><span style=color:#7f8989;># same as: date +%F or date -I
@@ -30,4 +30,13 @@
3030
</span><span>
3131
</span><span>$ date </span><span style=color:#72ab00;>-</span><span>d @</span><span style=color:#b3933a;>1722422393 </span><span style=color:#72ab00;>+</span><span style=color:#d07711;>'</span><span style=color:#aeb52b;>%F %T</span><span style=color:#d07711;>'
3232
</span><span style=color:#b3933a;>2024</span><span style=color:#72ab00;>-</span><span style=color:#b3933a;>07</span><span style=color:#72ab00;>-</span><span style=color:#b3933a;>31 16</span><span style=color:#72ab00;>:</span><span style=color:#b3933a;>09</span><span style=color:#72ab00;>:</span><span style=color:#b3933a;>53
33+
</span></code></pre><br><h2 id=date-arithmetic>Date arithmetic</h2><pre class=language-ruby data-lang=ruby style=background-color:#f5f5f5;color:#1f1f1f;><code class=language-ruby data-lang=ruby><span>$ date </span><span style=color:#72ab00;>-</span><span style=color:#5597d6;>I
34+
</span><span style=color:#b3933a;>2024</span><span style=color:#72ab00;>-</span><span style=color:#b3933a;>08</span><span style=color:#72ab00;>-</span><span style=color:#b3933a;>02
35+
</span><span>$ date </span><span style=color:#72ab00;>-</span><span>d </span><span style=color:#d07711;>'+1 month 4 days'
36+
</span><span style=color:#5597d6;>Friday </span><span style=color:#b3933a;>06 </span><span style=color:#5597d6;>September </span><span style=color:#b3933a;>2024 01</span><span style=color:#72ab00;>:</span><span style=color:#b3933a;>24</span><span style=color:#72ab00;>:</span><span style=color:#b3933a;>44 </span><span style=color:#5597d6;>PM IST
37+
</span><span>
38+
</span><span style=color:#7f8989;># same as: date -d '-20 days' +%F
39+
</span><span style=color:#7f8989;># you can also use '20 days ago'
40+
</span><span>$ date </span><span style=color:#72ab00;>-</span><span style=color:#5597d6;>I </span><span style=color:#72ab00;>-</span><span>d </span><span style=color:#d07711;>'-20 days'
41+
</span><span style=color:#b3933a;>2024</span><span style=color:#72ab00;>-</span><span style=color:#b3933a;>07</span><span style=color:#72ab00;>-</span><span style=color:#b3933a;>13
3342
</span></code></pre><br><h2 id=resource-links>Resource links</h2><ul><li><a href=https://www.gnu.org/software/coreutils/manual/coreutils.html#date-invocation>GNU date command manual</a><li><a href="https://unix.stackexchange.com/questions/tagged/date?tab=Votes">Top voted Q&A on unix.stackexchange for the date command tag</a></ul></div><div class=post-footer><div class=post-tags><a href=https://learnbyexample.github.io/tags/linux/>#linux</a><a href=https://learnbyexample.github.io/tags/command-line/>#command-line</a><a href=https://learnbyexample.github.io/tags/date/>#date</a></div><hr color=#e6e6e6><div class=post-nav><p><a class=next href=https://learnbyexample.github.io/mini/cli-text-editing-with-ed/>CLI text editing with ed →</a><br></div><hr color=#e6e6e6><p>📰 Use <a href=https://learnbyexample.github.io/atom.xml>this link</a> for the Atom feed. <br> ✅ Follow me on <a href=https://twitter.com/learn_byexample>Twitter</a>, <a href=https://github.com/learnbyexample>GitHub</a> and <a href=https://www.youtube.com/c/learnbyexample42>Youtube</a> for interesting tech nuggets. <br> 📧 Subscribe to <a href=https://learnbyexample.gumroad.com/l/learnbyexample-weekly>learnbyexample weekly</a> for programming resources, tips, tools, free ebooks and more (free newsletter, delivered every Friday).<hr color=#e6e6e6></div></article></div></main></div><script src=https://learnbyexample.github.io/even.js></script>

sitemap.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -144,7 +144,7 @@
144144
</url>
145145
<url>
146146
<loc>https://learnbyexample.github.io/mini/linux-date-command-examples/</loc>
147-
<lastmod>2024-07-31</lastmod>
147+
<lastmod>2024-08-02</lastmod>
148148
</url>
149149
<url>
150150
<loc>https://learnbyexample.github.io/mini/perl-oneliner-articles/</loc>

0 commit comments

Comments
 (0)