<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en-us"><generator uri="https://gohugo.io/" version="0.101.0">Hugo</generator><title type="html">format on A Scripter's Notes</title><subtitle type="html">Emacs, scripting and anything text oriented.</subtitle><link href="https://scripter.co/tags/format/" rel="alternate" type="text/html" title="HTML"/><link href="https://scripter.co/tags/format/index.xml" rel="alternate" type="application/rss+xml" title="RSS"/><link href="https://scripter.co/tags/format/atom.xml" rel="self" type="application/atom+xml" title="Atom"/><link href="https://scripter.co/tags/format/jf2feed.json" rel="alternate" type="application/jf2feed+json" title="jf2feed"/><updated>2026-04-22T08:24:58-04:00</updated><author><name>Kaushal Modi</name><email>kaushal.modi@gmail.com</email></author><id>https://scripter.co/tags/format/</id><entry><title type="html">Time formatting in Go</title><link href="https://scripter.co/time-formatting-in-go/?utm_source=atom_feed" rel="alternate" type="text/html"/><link href="https://scripter.co/follow-up-golang-quirk-number-strings-starting-with-0-are-octals/?utm_source=atom_feed" rel="related" type="text/html" title='  Follow-up: Golang Quirk: Number-strings starting with "0" are Octals   '/><link href="https://scripter.co/golang-quirk-number-strings-starting-with-0-are-octals/?utm_source=atom_feed" rel="related" type="text/html" title='  Golang Quirk: Number-strings starting with "0" are Octals   '/><link href="https://scripter.co/convert-seconds-to-human-time/?utm_source=atom_feed" rel="related" type="text/html" title="Your car will be ready in 8000 seconds"/><link href="https://scripter.co/installing-go-toolchain/?utm_source=atom_feed" rel="related" type="text/html" title="Installing go toolchain"/><id>https://scripter.co/time-formatting-in-go/</id><author><name>Kaushal Modi</name></author><published>2018-06-12T12:21:00-04:00</published><updated>2018-06-12T12:21:00-04:00</updated><content type="html"><![CDATA[<blockquote>A little cheat sheet to help remember the Go time formatting syntax.</blockquote><div class="ox-hugo-toc toc">
<div class="heading">Table of Contents</div>
<ul>
<li><a href="#time-dot-format-syntax"><code>Time.Format</code> Syntax</a></li>
<li><a href="#hugo-s-dateformat">Hugo&rsquo;s <code>dateFormat</code></a>
<ul>
<li><a href="#ordinal-dates">Ordinal dates</a></li>
</ul>
</li>
<li><a href="#examples">Examples</a></li>
<li><a href="#references">References</a></li>
</ul>
</div>
<!--endtoc-->
<p>Today on Hugo Discourse, I came across the &ndash; <em>Why &ldquo;2006&rdquo;?</em> &ndash; question
regarding the use of <code>{{ now.Format &quot;2006&quot; }}</code> in Hugo template. That
template simply prints the current year i.e. the time when that
template was rendered by Hugo.</p>
<p>As I was <a href="https://discourse.gohugo.io/t/how-do-i-display-the-current-year/1174/12?u=kaushalmodi">answering that</a>, I thought that this was a good time to
document this for myself too.. and thus this post.</p>

<h2 id="time-dot-format-syntax"><code>Time.Format</code> Syntax&nbsp;<a class="headline-hash no-text-decoration" href="#time-dot-format-syntax">#</a></h2>


<p>Hugo uses the <a href="https://golang.org/pkg/time/#Time.Format">Go <code>Time.Format</code></a> <em>method</em><sup id="fnref:1"><a href="#fn:1" class="footnote-ref" role="doc-noteref">1</a></sup> for formatting date and
time strings. But that format specification uses a strange syntax &mdash;
You need to associate 1, 2, 3, 4, 5, 6, 7 with date elements as
follows:</p>
<p><a id="table--go-time-format-syntax"></a></p>
<div class="table-caption">
  <span class="table-number"><a href="#table--go-time-format-syntax">Table 1</a>:</span>
  Cryptic dates used for Go <code>Time.Format</code>
</div>
<table>
<thead>
<tr>
<th>Number</th>
<th>Field Strings</th>
<th>Meaning</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>01 (<em>with optional leading zero</em>), 1, Jan, January</td>
<td>month</td>
</tr>
<tr>
<td>2</td>
<td>02 (<em>with optional leading zero</em>), 2, Mon, Monday</td>
<td>day</td>
</tr>
<tr>
<td>3</td>
<td>03 (<em>with optional leading zero</em>), 3, 15</td>
<td>hour</td>
</tr>
<tr>
<td>4</td>
<td>04 (<em>with optional leading zero</em>), 4</td>
<td>minute</td>
</tr>
<tr>
<td>5</td>
<td>05 (<em>with optional leading zero</em>), 5</td>
<td>seconds</td>
</tr>
<tr>
<td>6</td>
<td>06, 2006</td>
<td>year</td>
</tr>
<tr>
<td>-7</td>
<td>-0700, -07:00, -07, MST</td>
<td>timezone</td>
</tr>
<tr>
<td>n/a</td>
<td>PM (<strong>not AM</strong>)</td>
<td>AM or PM (<em>uppercase</em>)</td>
</tr>
<tr>
<td>n/a</td>
<td>pm (<strong>not am</strong>)</td>
<td>am or pm (<em>lowercase</em>)</td>
</tr>
<tr>
<td>n/a</td>
<td><strong>anything else</strong></td>
<td><em>shows up as it is</em></td>
</tr>
</tbody>
</table>
<p>You can visualize a formatting string <code>&quot;Jan 2 15:04:05 2006 MST&quot;</code> as
below:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">Jan 2 15:04:05 2006 MST
</span></span><span class="line"><span class="cl">  1 2  3  4  5    6  -7
</span></span></code></pre></div>
<h2 id="hugo-s-dateformat">Hugo&rsquo;s <code>dateFormat</code>&nbsp;<a class="headline-hash no-text-decoration" href="#hugo-s-dateformat">#</a></h2>


<p>The <code>dateFormat</code> accepts a date/time format using the same syntax.</p>
<p>Here is the signature of that function:</p>
<div class="highlight"><pre tabindex="0" class="chroma"><code class="language-text" data-lang="text"><span class="line"><span class="cl">dateFormat &#34;&lt;FORMAT_STRING&gt;&#34; &#34;&lt;DATE_RFC3339&gt;&#34;
</span></span></code></pre></div><ul>
<li>The <code>&lt;FORMAT_STRING&gt;</code> uses one or more date/time fields as shown in
the &ldquo;Field Strings&rdquo; column in <a href="#table--go-time-format-syntax">Table 1</a>. Few
examples:
<ul>
<li><em>&ldquo;Jan 2 15:04:05 2006 MST&rdquo;</em></li>
<li><em>&ldquo;2006-01-02&rdquo;</em></li>
<li><em>&ldquo;Jan 2, Mon&rdquo;</em></li>
<li><em>&ldquo;It&rsquo;s 3 O&rsquo;clock&rdquo;</em></li>
</ul>
</li>
<li>The <code>&lt;DATE_RFC3339&gt;</code> is a date string compatible with <a href="https://tools.ietf.org/html/rfc3339#section-5.8">RFC3339</a>. Few
example valid date strings:
<ul>
<li><em>&ldquo;2017-07-31&rdquo;</em></li>
<li><em>&ldquo;2017-07-31T17:05:38&rdquo;</em></li>
<li><em>&ldquo;2017-07-31T17:05:38Z&rdquo;</em></li>
<li><em>&ldquo;2017-07-31T17:05:38+04:00&rdquo;</em></li>
<li><em>&ldquo;2017-07-31T17:05:38-04:00&rdquo;</em></li>
</ul>
</li>
</ul>

<h3 id="ordinal-dates">Ordinal dates&nbsp;<a class="headline-hash no-text-decoration" href="#ordinal-dates">#</a></h3>


<p>In addition, <a href="https://gohugo.io/functions/humanize/"><code>humanize</code></a> can be used to generate <em>ordinal</em> date, like
&ldquo;1st&rdquo;, &ldquo;2nd&rdquo;, &ldquo;3rd&rdquo;, ..</p>

<h2 id="examples">Examples&nbsp;<a class="headline-hash no-text-decoration" href="#examples">#</a></h2>



The time string <i>"2018-06-13T11:30:00-04:00"</i>
(RFC3339 format) can be output in different formats:

<br />
<br />

<table>
    <tr><th>Go Formatting string</th>
        <th>Output</th>
    </tr>
    
        
        <tr><td><code>"Jan 2 15:04:05 2006 MST"</code></td><td><code>"Jun 13 11:30:00 2018 EDT"</code></td></tr>
    
        
        <tr><td><code>"2006-01-02 03:04pm Monday"</code></td><td><code>"2018-06-13 11:30am Wednesday"</code></td></tr>
    
        
        <tr><td><code>"Jan 2, Mon"</code></td><td><code>"Jun 13, Wed"</code></td></tr>
    
        
        <tr><td><code>"It's 3 O'clock"</code></td><td><code>"It's 11 O'clock"</code></td></tr>
    
</table>

Below shows examples of printing ordinal dates using a combination of
<code>((dateFormat "2" $dt) | humanize)</code> and <code>(dateFormat "of
January" $dt)</code>:

<br />
<br />

<table>
    <tr><th>Date (<code>$dt</code>)</th>
        <th>Output</th>
    </tr>
    
        
        <tr><td>2018-06-01</td><td><code>"1st of June"</code></td></tr>
    
        
        <tr><td>2018-06-02</td><td><code>"2nd of June"</code></td></tr>
    
        
        <tr><td>2018-06-03</td><td><code>"3rd of June"</code></td></tr>
    
        
        <tr><td>2018-06-04</td><td><code>"4th of June"</code></td></tr>
    
        
        <tr><td>2018-06-10</td><td><code>"10th of June"</code></td></tr>
    
</table>


<h2 id="references">References&nbsp;<a class="headline-hash no-text-decoration" href="#references">#</a></h2>


<ul>
<li>gohugohq.com &ndash; <a href="https://gohugohq.com/howto/hugo-dateformat/">Hugo dateFormat</a></li>
<li>Hugo documentation &ndash; <a href="https://gohugo.io/functions/dateformat/"><code>dateFormat</code></a></li>
<li><a href="https://github.com/bdotdub/fuckinggodateformat">f***inggodateformat</a></li>
</ul>
<div class="footnotes" role="doc-endnotes">
<hr>
<ol>
<li id="fn:1">
<p>I am not a Go dev.. let me know if the correct term for that is
a &ldquo;function&rdquo; or a &ldquo;method&rdquo; in Go 😄. <strong>Update</strong> <span class="timestamp-wrapper"><span class="timestamp">&lt;2018-11-08 Thu&gt;</span></span>:
Thanks to <em>Mendy</em> in comments, I have now fixed referencing
<code>Time.Format</code> as a <strong>method</strong>.&#160;<a href="#fnref:1" class="footnote-backref" role="doc-backlink">&#x21a9;&#xfe0e;</a></p>
</li>
</ol>
</div>
]]></content><category scheme="https://scripter.co/categories/hugo" term="hugo" label="hugo"/><category scheme="https://scripter.co/tags/time" term="time" label="time"/><category scheme="https://scripter.co/tags/format" term="format" label="format"/><category scheme="https://scripter.co/tags/golang" term="golang" label="golang"/></entry></feed>