hugo/docs/content/en/functions/dateformat.md
Bjørn Erik Pedersen a57dda854b Localize time.Format
Fixes #8797
2021-07-27 19:02:48 +02:00

1.8 KiB
Raw Blame History

title description godocref date publishdate lastmod categories menu keywords signature workson hugoversion relatedfuncs deprecated
time.Format Converts a date/time to a localized string. https://golang.org/pkg/time/ 2017-02-01 2017-02-01 2017-02-01
functions
docs
parent
functions
dates
time
strings
time.Format LAYOUT INPUT
Format
now
Unix
time
false

time.Format (alias dateFormat) converts either a time.Time object (e.g. .Date) or a timestamp string INPUT into the format specified by the LAYOUT string.

{{ time.Format "Monday, Jan 2, 2006" "2015-01-21" }} → "Wednesday, Jan 21, 2015"

Note that since Hugo 0.87.0, time.Format will return a localized string for the currrent language. {{< new-in "0.87.0" >}}

The LAYOUT string can be either:

  • Gos Layout String to learn about how the LAYOUT string has to be formatted. There are also some useful examples.
  • A custom Hugo layout identifier (see full list below)

See the time function to convert a timestamp string to a Go time.Time type value.

Date/time formatting layouts

{{< new-in "0.87.0" >}}

Go's date layout strings can be hard to reason about, especially with multiple languages. Since Hugo 0.87.0 you can alternatively use some predefined layout idenfifiers that will output localized dates or times:

{{ .Date | time.Format ":date_long" }}

The full list of custom layouts with examples for English:

  • :date_full => Wednesday, June 6, 2018

  • :date_long => June 6, 2018

  • :date_medium => Jun 6, 2018

  • :date_short => 6/6/18

  • :time_full => 2:09:37 am UTC

  • :time_long => 2:09:37 am UTC

  • :time_medium => 2:09:37 am

  • :time_short => 2:09 am