time.Format
Syntax
time.Format LAYOUT INPUT ⟼ string
Alias
dateFormat
{{ $t := "2023-01-27T23:44:58-08:00" }}
{{ $format := "2 Jan 2006" }}
{{ $t | time.Format $format }} → 27 Jan 2023
{{ $t = time.AsTime $t }}
{{ $t | time.Format $format }} → 27 Jan 2023
Layout string
Format a time.Time
value based on Go’s reference time:
Mon Jan 2 15:04:05 MST 2006
Create a format string using these components:
Description | Valid components |
---|---|
Year | "2006" "06" |
Month | "Jan" "January" "01" "1" |
Day of the week | "Mon" "Monday" |
Day of the month | "2" "_2" "02" |
Day of the year | "__2" "002" |
Hour | "15" "3" "03" |
Minute | "4" "04" |
Second | "5" "05" |
AM/PM mark | "PM" |
Time zone offsets | "-0700" "-07:00" "-07" "-070000" "-07:00:00" |
Replace the sign in the format string with a Z to print Z instead of an offset for the UTC zone.
Description | Valid components |
---|---|
Time zone offsets | "Z0700" "Z07:00" "Z07" "Z070000" "Z07:00:00" |
{{ $t := "2023-01-27T23:44:58-08:00" }}
{{ $t = time.AsTime $t }}
{{ $t = $t.Format "Jan 02, 2006 3:04 PM Z07:00" }}
{{ $t }} → Jan 27, 2023 11:44 PM -08:00
Strings such as PST
and CET
are not time zones. They are time zone abbreviations.
Strings such as -07:00
and +01:00
are not time zones. They are time zone offsets.
A time zone is a geographic area with the same local time. For example, the time zone abbreviated by PST
and PDT
(depending on Daylight Savings Time) is America/Los_Angeles
.
Localization
Use the time.Format
function to localize time.Time
values for the current language and region.
Use the layout string as described above, or one of the tokens below. For example:
{{ .Date | time.Format ":date_medium" }} → Jan 27, 2023
Localized to en-US:
Token | Result |
---|---|
:date_full |
Friday, January 27, 2023 |
:date_long |
January 27, 2023 |
:date_medium |
Jan 27, 2023 |
:date_short |
1/27/23 |
:time_full |
11:44:58 pm Pacific Standard Time |
:time_long |
11:44:58 pm PST |
:time_medium |
11:44:58 pm |
:time_short |
11:44 pm |
Localized to de-DE:
Token | Result |
---|---|
:date_full |
Freitag, 27. Januar 2023 |
:date_long |
27. Januar 2023 |
:date_medium |
27.01.2023 |
:date_short |
27.01.23 |
:time_full |
23:44:58 Nordamerikanische Westküsten-Normalzeit |
:time_long |
23:44:58 PST |
:time_medium |
23:44:58 |
:time_short |
23:44 |