<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>format on
A Scripter's Notes</title><link>https://scripter.co/tags/format/</link><description>Recent content in format
on A Scripter's Notes</description><language>en-us</language><managingEditor>kaushal.modi@gmail.com (Kaushal Modi)</managingEditor><webMaster>kaushal.modi@gmail.com (Kaushal Modi)</webMaster><lastBuildDate>Wed, 22 Apr 2026 08:24:58 -0400</lastBuildDate><generator>Hugo -- gohugo.io</generator><docs>https://validator.w3.org/feed/docs/rss2.html</docs><atom:link href="https://scripter.co/tags/format/index.xml" rel="self" type="application/rss+xml"/><item><title>Time formatting in Go</title><link>https://scripter.co/time-formatting-in-go/</link><description>&lt;blockquote>A little cheat sheet to help remember the Go time formatting syntax.&lt;/blockquote>&lt;div class="ox-hugo-toc toc">
&lt;div class="heading">Table of Contents&lt;/div>
&lt;ul>
&lt;li>&lt;a href="#time-dot-format-syntax">&lt;code>Time.Format&lt;/code> Syntax&lt;/a>&lt;/li>
&lt;li>&lt;a href="#hugo-s-dateformat">Hugo&amp;rsquo;s &lt;code>dateFormat&lt;/code>&lt;/a>
&lt;ul>
&lt;li>&lt;a href="#ordinal-dates">Ordinal dates&lt;/a>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>&lt;a href="#examples">Examples&lt;/a>&lt;/li>
&lt;li>&lt;a href="#references">References&lt;/a>&lt;/li>
&lt;/ul>
&lt;/div>
&lt;!--endtoc-->
&lt;p>Today on Hugo Discourse, I came across the &amp;ndash; &lt;em>Why &amp;ldquo;2006&amp;rdquo;?&lt;/em> &amp;ndash; question
regarding the use of &lt;code>{{ now.Format &amp;quot;2006&amp;quot; }}&lt;/code> in Hugo template. That
template simply prints the current year i.e. the time when that
template was rendered by Hugo.&lt;/p>
&lt;p>As I was &lt;a href="https://discourse.gohugo.io/t/how-do-i-display-the-current-year/1174/12?u=kaushalmodi">answering that&lt;/a>, I thought that this was a good time to
document this for myself too.. and thus this post.&lt;/p>
&lt;h2 id="time-dot-format-syntax">&lt;code>Time.Format&lt;/code> Syntax&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#time-dot-format-syntax">#&lt;/a>&lt;/h2>
&lt;p>Hugo uses the &lt;a href="https://golang.org/pkg/time/#Time.Format">Go &lt;code>Time.Format&lt;/code>&lt;/a> &lt;em>method&lt;/em>&lt;sup id="fnref:1">&lt;a href="#fn:1" class="footnote-ref" role="doc-noteref">1&lt;/a>&lt;/sup> for formatting date and
time strings. But that format specification uses a strange syntax &amp;mdash;
You need to associate 1, 2, 3, 4, 5, 6, 7 with date elements as
follows:&lt;/p>
&lt;p>&lt;a id="table--go-time-format-syntax">&lt;/a>&lt;/p>
&lt;div class="table-caption">
&lt;span class="table-number">&lt;a href="#table--go-time-format-syntax">Table 1&lt;/a>:&lt;/span>
Cryptic dates used for Go &lt;code>Time.Format&lt;/code>
&lt;/div>
&lt;table>
&lt;thead>
&lt;tr>
&lt;th>Number&lt;/th>
&lt;th>Field Strings&lt;/th>
&lt;th>Meaning&lt;/th>
&lt;/tr>
&lt;/thead>
&lt;tbody>
&lt;tr>
&lt;td>1&lt;/td>
&lt;td>01 (&lt;em>with optional leading zero&lt;/em>), 1, Jan, January&lt;/td>
&lt;td>month&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>2&lt;/td>
&lt;td>02 (&lt;em>with optional leading zero&lt;/em>), 2, Mon, Monday&lt;/td>
&lt;td>day&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>3&lt;/td>
&lt;td>03 (&lt;em>with optional leading zero&lt;/em>), 3, 15&lt;/td>
&lt;td>hour&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>4&lt;/td>
&lt;td>04 (&lt;em>with optional leading zero&lt;/em>), 4&lt;/td>
&lt;td>minute&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>5&lt;/td>
&lt;td>05 (&lt;em>with optional leading zero&lt;/em>), 5&lt;/td>
&lt;td>seconds&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>6&lt;/td>
&lt;td>06, 2006&lt;/td>
&lt;td>year&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>-7&lt;/td>
&lt;td>-0700, -07:00, -07, MST&lt;/td>
&lt;td>timezone&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>n/a&lt;/td>
&lt;td>PM (&lt;strong>not AM&lt;/strong>)&lt;/td>
&lt;td>AM or PM (&lt;em>uppercase&lt;/em>)&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>n/a&lt;/td>
&lt;td>pm (&lt;strong>not am&lt;/strong>)&lt;/td>
&lt;td>am or pm (&lt;em>lowercase&lt;/em>)&lt;/td>
&lt;/tr>
&lt;tr>
&lt;td>n/a&lt;/td>
&lt;td>&lt;strong>anything else&lt;/strong>&lt;/td>
&lt;td>&lt;em>shows up as it is&lt;/em>&lt;/td>
&lt;/tr>
&lt;/tbody>
&lt;/table>
&lt;p>You can visualize a formatting string &lt;code>&amp;quot;Jan 2 15:04:05 2006 MST&amp;quot;&lt;/code> as
below:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="cl">Jan 2 15:04:05 2006 MST
&lt;/span>&lt;/span>&lt;span class="line">&lt;span class="cl"> 1 2 3 4 5 6 -7
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>
&lt;h2 id="hugo-s-dateformat">Hugo&amp;rsquo;s &lt;code>dateFormat&lt;/code>&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#hugo-s-dateformat">#&lt;/a>&lt;/h2>
&lt;p>The &lt;code>dateFormat&lt;/code> accepts a date/time format using the same syntax.&lt;/p>
&lt;p>Here is the signature of that function:&lt;/p>
&lt;div class="highlight">&lt;pre tabindex="0" class="chroma">&lt;code class="language-text" data-lang="text">&lt;span class="line">&lt;span class="cl">dateFormat &amp;#34;&amp;lt;FORMAT_STRING&amp;gt;&amp;#34; &amp;#34;&amp;lt;DATE_RFC3339&amp;gt;&amp;#34;
&lt;/span>&lt;/span>&lt;/code>&lt;/pre>&lt;/div>&lt;ul>
&lt;li>The &lt;code>&amp;lt;FORMAT_STRING&amp;gt;&lt;/code> uses one or more date/time fields as shown in
the &amp;ldquo;Field Strings&amp;rdquo; column in &lt;a href="#table--go-time-format-syntax">Table 1&lt;/a>. Few
examples:
&lt;ul>
&lt;li>&lt;em>&amp;ldquo;Jan 2 15:04:05 2006 MST&amp;rdquo;&lt;/em>&lt;/li>
&lt;li>&lt;em>&amp;ldquo;2006-01-02&amp;rdquo;&lt;/em>&lt;/li>
&lt;li>&lt;em>&amp;ldquo;Jan 2, Mon&amp;rdquo;&lt;/em>&lt;/li>
&lt;li>&lt;em>&amp;ldquo;It&amp;rsquo;s 3 O&amp;rsquo;clock&amp;rdquo;&lt;/em>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;li>The &lt;code>&amp;lt;DATE_RFC3339&amp;gt;&lt;/code> is a date string compatible with &lt;a href="https://tools.ietf.org/html/rfc3339#section-5.8">RFC3339&lt;/a>. Few
example valid date strings:
&lt;ul>
&lt;li>&lt;em>&amp;ldquo;2017-07-31&amp;rdquo;&lt;/em>&lt;/li>
&lt;li>&lt;em>&amp;ldquo;2017-07-31T17:05:38&amp;rdquo;&lt;/em>&lt;/li>
&lt;li>&lt;em>&amp;ldquo;2017-07-31T17:05:38Z&amp;rdquo;&lt;/em>&lt;/li>
&lt;li>&lt;em>&amp;ldquo;2017-07-31T17:05:38+04:00&amp;rdquo;&lt;/em>&lt;/li>
&lt;li>&lt;em>&amp;ldquo;2017-07-31T17:05:38-04:00&amp;rdquo;&lt;/em>&lt;/li>
&lt;/ul>
&lt;/li>
&lt;/ul>
&lt;h3 id="ordinal-dates">Ordinal dates&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#ordinal-dates">#&lt;/a>&lt;/h3>
&lt;p>In addition, &lt;a href="https://gohugo.io/functions/humanize/">&lt;code>humanize&lt;/code>&lt;/a> can be used to generate &lt;em>ordinal&lt;/em> date, like
&amp;ldquo;1st&amp;rdquo;, &amp;ldquo;2nd&amp;rdquo;, &amp;ldquo;3rd&amp;rdquo;, ..&lt;/p>
&lt;h2 id="examples">Examples&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#examples">#&lt;/a>&lt;/h2>
The time string &lt;i>"2018-06-13T11:30:00-04:00"&lt;/i>
(RFC3339 format) can be output in different formats:
&lt;br />
&lt;br />
&lt;table>
&lt;tr>&lt;th>Go Formatting string&lt;/th>
&lt;th>Output&lt;/th>
&lt;/tr>
&lt;tr>&lt;td>&lt;code>"Jan 2 15:04:05 2006 MST"&lt;/code>&lt;/td>&lt;td>&lt;code>"Jun 13 11:30:00 2018 EDT"&lt;/code>&lt;/td>&lt;/tr>
&lt;tr>&lt;td>&lt;code>"2006-01-02 03:04pm Monday"&lt;/code>&lt;/td>&lt;td>&lt;code>"2018-06-13 11:30am Wednesday"&lt;/code>&lt;/td>&lt;/tr>
&lt;tr>&lt;td>&lt;code>"Jan 2, Mon"&lt;/code>&lt;/td>&lt;td>&lt;code>"Jun 13, Wed"&lt;/code>&lt;/td>&lt;/tr>
&lt;tr>&lt;td>&lt;code>"It's 3 O'clock"&lt;/code>&lt;/td>&lt;td>&lt;code>"It's 11 O'clock"&lt;/code>&lt;/td>&lt;/tr>
&lt;/table>
Below shows examples of printing ordinal dates using a combination of
&lt;code>((dateFormat "2" $dt) | humanize)&lt;/code> and &lt;code>(dateFormat "of
January" $dt)&lt;/code>:
&lt;br />
&lt;br />
&lt;table>
&lt;tr>&lt;th>Date (&lt;code>$dt&lt;/code>)&lt;/th>
&lt;th>Output&lt;/th>
&lt;/tr>
&lt;tr>&lt;td>2018-06-01&lt;/td>&lt;td>&lt;code>"1st of June"&lt;/code>&lt;/td>&lt;/tr>
&lt;tr>&lt;td>2018-06-02&lt;/td>&lt;td>&lt;code>"2nd of June"&lt;/code>&lt;/td>&lt;/tr>
&lt;tr>&lt;td>2018-06-03&lt;/td>&lt;td>&lt;code>"3rd of June"&lt;/code>&lt;/td>&lt;/tr>
&lt;tr>&lt;td>2018-06-04&lt;/td>&lt;td>&lt;code>"4th of June"&lt;/code>&lt;/td>&lt;/tr>
&lt;tr>&lt;td>2018-06-10&lt;/td>&lt;td>&lt;code>"10th of June"&lt;/code>&lt;/td>&lt;/tr>
&lt;/table>
&lt;h2 id="references">References&amp;nbsp;&lt;a class="headline-hash no-text-decoration" href="#references">#&lt;/a>&lt;/h2>
&lt;ul>
&lt;li>gohugohq.com &amp;ndash; &lt;a href="https://gohugohq.com/howto/hugo-dateformat/">Hugo dateFormat&lt;/a>&lt;/li>
&lt;li>Hugo documentation &amp;ndash; &lt;a href="https://gohugo.io/functions/dateformat/">&lt;code>dateFormat&lt;/code>&lt;/a>&lt;/li>
&lt;li>&lt;a href="https://github.com/bdotdub/fuckinggodateformat">f***inggodateformat&lt;/a>&lt;/li>
&lt;/ul>
&lt;div class="footnotes" role="doc-endnotes">
&lt;hr>
&lt;ol>
&lt;li id="fn:1">
&lt;p>I am not a Go dev.. let me know if the correct term for that is
a &amp;ldquo;function&amp;rdquo; or a &amp;ldquo;method&amp;rdquo; in Go 😄. &lt;strong>Update&lt;/strong> &lt;span class="timestamp-wrapper">&lt;span class="timestamp">&amp;lt;2018-11-08 Thu&amp;gt;&lt;/span>&lt;/span>:
Thanks to &lt;em>Mendy&lt;/em> in comments, I have now fixed referencing
&lt;code>Time.Format&lt;/code> as a &lt;strong>method&lt;/strong>.&amp;#160;&lt;a href="#fnref:1" class="footnote-backref" role="doc-backlink">&amp;#x21a9;&amp;#xfe0e;&lt;/a>&lt;/p>
&lt;/li>
&lt;/ol>
&lt;/div></description><author>Kaushal.Modi@fakeEmailToMakeValidatorHappy.com (Kaushal Modi)</author><category domain="https://scripter.co/categories/hugo">hugo</category><category domain="https://scripter.co/tags/time">time</category><category domain="https://scripter.co/tags/format">format</category><category domain="https://scripter.co/tags/golang">golang</category><guid>https://scripter.co/time-formatting-in-go/</guid><pubDate>Tue, 12 Jun 2018 12:21:00 -0400</pubDate></item></channel></rss>