CTO and co-founder of Signal Sciences. Author and speaker on software engineering, devops, and security.

Convert strftime Time Format to Hugo

Convert time formats from other blogging platforms to Hugo.

Many blogging platforms, such as Wordpress and Jekyll, use some variation of strptime to format date and time values. Hugo does time formatting in a completely different way and is based on the time package from Go. It uses the concept of a magic date as represented by:

Mon Jan 2 15:04:05 MST 2006

or

01/02 03:04:05PM '06 -0700

Notice how it’s 1, 2, 3, 4, 5, 6, 7 for month, day, hour, minute, second, year, and time zone offset.

To print your time, just re-arrange, or edit one of these magic times any way you want. Just want to print the month? The format is Jan, January, 1, or 01 depending on how you want to format it. The real month will be filled in. The same goes for all the other parts of the time.

Once you get the hang of it, you'll find January 2, 2006 a lot clearer than %b %e, %Y.

The table below translates all strptime formats to Hugo .

strftime hugo / golang description
%a Mon Abbreviated weekday name
%A Monday Full weekday name
%b Jan Abbreviated month
%B January Full month name
%c Note 1 Preferred local datetime
%C Note 1 Century number
%d 02 Day of month 01 to 31
%D 02/01/06 USA month/day/year
%e 2 Day of month, no leading zero
%E TBD Modifier, use alternative format
%g Note 2 ISO week-based year, without century
%G Note 2 ISO week-based year, with century
%F 2006-01-02 ISO 8061 date
%h Jan Abbreviated month (same as %b)
%H 15 Hour 00 to 23
%I 03 Hour 00 to 12
%j Note 2 Day of year 001 to 365
%k 3 Hour ' 0' to '23' with leading space
%l 4 Hour ' 0' to '12' with leading space
%m 01 Month number 01 to 12
%M 04 Minute 01 to 59
%n \n Newline character
%O TBD Modifier, use alternative format
%p PM “AM” or “PM”
%P Note 3 “am” or “pm”
%r 03:04:05 PM same as `%I:%M:%S %p`
%R 15:04 same as `%H:%M`
%s Note 4 Seconds since Unix epoch
%S 05 Second of hour, 00 to 60
%t \t Tab character
%x Note 1 Preferred
%X Note 1 Preferred
%y 06 Year without century
%Y 2006 Year with century
%z -0700 +/-hhmm time zone offset
%Z MST Time zone abbreviation
%% % “%” character

Note 1 - "Preferred" locale-specific formats

There is no concept of a "preferred locale" in Hugo at this time. Use an explicit format instead. See Create a Multilingual Site for more details.

Note 2 - Week or Day of year

These can be computed using Day and ISOWeek methods of the Time object.

Note 3 - Lower-case versions of AM or PM

Hugo only supports AM or PM. To do something different, create a custom implementation:

{{ if .Date.Hour < 12 -}}
ante meridiem
{{- else -}}
post meridiem
{{- end }}

Note 4 - Unix timestamps

Instead of a time format, use the UTC and Unix methods of the Time object:

{{ .Date.UTC.Unix }}

Common Time Formats

Below are commonly used time and date formats.

RFC 3339, ISO 8061

This format is used in Open Graph, and Google Schemas. RFC 3339 and ISO 8061 defines a number of variations, but the following should work:

{{ .Date.Format "2006-01-02T15:04:05Z07:00" }}

RFC 1123 HTTP dates

RFC 1123 is used in various HTTP headers. Note that it uses the old GMT time zone instead of UTC.

{{ .Date.UTC.Format "Mon, 02 Jan 2006 15:04:05 GMT" }}
hugo

© 2018 Nick Galbreath