time.Format
Syntax
Returns
Alias
Use the time.Format function with time.Time values:
{{ $t := time.AsTime "2023-10-15T13:18:50-07:00" }}
{{ time.Format "2 Jan 2006" $t }} → 15 Oct 2023Or use time.Format with a parsable string representation of a date/time value:
{{ $t := "15 Oct 2023" }}
{{ time.Format "January 2, 2006" $t }} → October 15, 2023Examples of parsable string representations:
| 格式 | 時區 |
|---|---|
2023-10-15T13:18:50-07:00 |
America/Los_Angeles |
2023-10-15T13:18:50-0700 |
America/Los_Angeles |
2023-10-15T13:18:50Z |
Etc/UTC |
2023-10-15T13:18:50 |
默認為 Etc/UTC |
2023-10-15 |
默認為 Etc/UTC |
15 Oct 2023 |
默認為 Etc/UTC |
最後三個示例不是完全限定的,默認為 Etc/UTC 時區。
To override the default time zone, set the timeZone in your site configuration. The order of precedence for determining the time zone is:
- The time zone offset in the date/time string
- The time zone specified in your site configuration
- The
Etc/UTCtime zone
Layout string
基於 Go 的參考時間 格式化 time.Time 值:
Mon Jan 2 15:04:05 MST 2006使用這些組件創建布局字符串:
| 描述 | 有效組件 |
|---|---|
| 年 | "2006" "06" |
| 月 | "Jan" "January" "01" "1" |
| 星期幾 | "Mon" "Monday" |
| 月份中的日期 | "2" "_2" "02" |
| 一年中的第幾天 | "__2" "002" |
| 小時 | "15" "3" "03" |
| 分鐘 | "4" "04" |
| 秒 | "5" "05" |
| 上午/下午標記 | "PM" |
| 時區偏移 | "-0700" "-07:00" "-07" "-070000" "-07:00:00" |
將布局字符串中的符號替換為 Z 以打印 Z 而不是 UTC 時區的偏移。
| 描述 | 有效組件 |
|---|---|
| 時區偏移 | "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諸如 PST 和 CET 之類的字符串不是時區。它們是時區 縮寫。
諸如 -07:00 和 +01:00 之類的字符串不是時區。它們是時區 偏移。
時區是具有相同本地時間的地理區域。例如,由 PST 和 PDT(取決於夏令時)縮寫的時區是 America/Los_Angeles。
Localization
Use the time.Format function to localize time.Time values for the current language and region.
日期、貨幣、數字和百分比的本地化由 gohugoio/locales 包執行。當前站點的語言標簽必須與列出的語言區域之一匹配。
Use the layout string as described above, or one of the tokens below. For example:
{{ .Date | time.Format ":date_medium" }} → Jan 27, 2023Localized 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 |