HUGO

Formatting a date with suffix (2nd)

Hi all, I couldn’t find anything about formatting a date with a suffix so I thought I’d post my solution for future searchers to find. Using the following template code:

posted on {{ .Date.Format "2" }}{{ if in (slice 1 21 31) .Date.Day}}st{{ else if in (slice 2 22) .Date.Day}}nd{{ else if in (slice 3 23) .Date.Day}}rd{{ else }}th{{ end }} of {{ .Date.Format "January 2006" }}

will give you a result like so:

posted on 5th of March 2017

There may be an easier way to achieve this or ways to clean it up but it seems to work as intended. Hope this helps someone.

3 Likes