Hugo Verbose Dates With Suffixes

When building templates for the hugo static site generator you may find that you want to format post dates in a verbose format with a suffix, For example:

7th of April 2018

When building with hugo you essentially have access to golangs Time.Format function. Unfortunately there’s not built-in way to specify a day suffix such as 4th or 3rd with this date formatting.

To achieve this you can use a series of conditional rules that check the day number and then assign the correct suffix:

{{ .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" }}