mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-20 21:31:32 +02:00
Merge commit 'de0df119b504a91c9e1f442b07954f366ffb2932'
This commit is contained in:
@@ -21,41 +21,34 @@ toc: true
|
||||
Hugo provides [functions] and [methods] to format, localize, parse, compare, and manipulate date/time values. Before you can do any of these with string representations of date/time values, you must first convert them to [`time.Time`] values using the `time.AsTime` function.
|
||||
|
||||
```go-html-template
|
||||
{{ $t := "2023-10-15T14:20:28-07:00" }}
|
||||
{{ time.AsTime $t }} → 2023-10-15 14:20:28 -0700 PDT (time.Time)
|
||||
{{ $t := "2023-10-15T13:18:50-07:00" }}
|
||||
{{ time.AsTime $t }} → 2023-10-15 13:18:50 -0700 PDT (time.Time)
|
||||
```
|
||||
|
||||
## Parsable strings
|
||||
|
||||
As shown above, the first argument must be a *parsable* string representation of a date/time value. For example:
|
||||
As shown above, the first argument must be a parsable string representation of a date/time value. For example:
|
||||
|
||||
{{% include "functions/time/_common/parsable-date-time-strings.md" %}}
|
||||
|
||||
## Time zones
|
||||
To override the default time zone, set the [`timeZone`] in your site configuration or provide a second argument to the `time.AsTime` function. For example:
|
||||
|
||||
When the parsable string does not contain a time zone offset, you can do either of the following to assign a time zone other than Etc/UTC:
|
||||
```go-html-template
|
||||
{{ time.AsTime "15 Oct 2023" "America/Los_Angeles" }}
|
||||
```
|
||||
|
||||
- Provide a second argument to the `time.AsTime` function
|
||||
|
||||
```go-html-template
|
||||
{{ time.AsTime "15 Oct 2023" "America/Chicago" }}
|
||||
```
|
||||
|
||||
- Set the default time zone in your site configuration
|
||||
|
||||
{{< code-toggle file=hugo >}}
|
||||
timeZone = 'America/New_York'
|
||||
{{< /code-toggle >}}
|
||||
The list of valid time zones may be system dependent, but should include `UTC`, `Local`, or any location in the [IANA Time Zone database].
|
||||
|
||||
The order of precedence for determining the time zone is:
|
||||
|
||||
1. The time zone offset in the date/time string
|
||||
2. The time zone provide as the second argument to the `time.AsTime` function
|
||||
2. The time zone provided as the second argument to the `time.AsTime` function
|
||||
3. The time zone specified in your site configuration
|
||||
4. The `Etc/UTC` time zone
|
||||
|
||||
The list of valid time zones may be system dependent, but should include `UTC`, `Local`, or any location in the [IANA Time Zone database].
|
||||
|
||||
[IANA Time Zone database]: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
||||
[`time.Time`]: https://pkg.go.dev/time#Time
|
||||
[`timeZone`]: https://gohugo.io/getting-started/configuration/#timezone
|
||||
[functions]: /functions/time/
|
||||
[iana time zone database]: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
||||
[methods]: /methods/time/
|
||||
|
@@ -19,21 +19,29 @@ toc: true
|
||||
Use the `time.Format` function with `time.Time` values:
|
||||
|
||||
```go-html-template
|
||||
{{ $t := time.AsTime "2023-02-27T23:44:58-08:00" }}
|
||||
{{ time.Format "2 Jan 2006" $t }} → 27 Feb 2023
|
||||
{{ $t := time.AsTime "2023-10-15T13:18:50-07:00" }}
|
||||
{{ time.Format "2 Jan 2006" $t }} → 15 Oct 2023
|
||||
```
|
||||
|
||||
Or use `time.Format` with a *parsable* string representation of a date/time value:
|
||||
Or use `time.Format` with a parsable string representation of a date/time value:
|
||||
|
||||
```go-html-template
|
||||
{{ $t := "27 Feb 2023" }}
|
||||
{{ time.Format "January 2, 2006" $t }} → February 27, 2023
|
||||
{{ $t := "15 Oct 2023" }}
|
||||
{{ time.Format "January 2, 2006" $t }} → October 15, 2023
|
||||
```
|
||||
|
||||
Examples of parsable string representations:
|
||||
|
||||
{{% include "functions/time/_common/parsable-date-time-strings.md" %}}
|
||||
|
||||
To override the default time zone, set the [`timeZone`] in your site configuration. The order of precedence for determining the time zone is:
|
||||
|
||||
1. The time zone offset in the date/time string
|
||||
2. The time zone specified in your site configuration
|
||||
3. The `Etc/UTC` time zone
|
||||
|
||||
[`timeZone`]: https://gohugo.io/getting-started/configuration/#timezone
|
||||
|
||||
## Layout string
|
||||
|
||||
{{% include "functions/_common/time-layout-string.md" %}}
|
||||
|
@@ -2,13 +2,13 @@
|
||||
# Do not remove front matter.
|
||||
---
|
||||
|
||||
String representation|Time zone
|
||||
Format|Time zone
|
||||
:--|:--
|
||||
2023-10-15T14:20:28-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
|
||||
`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`|Default is `Etc/UTC`
|
||||
`2023-10-15`|Default is `Etc/UTC`
|
||||
`15 Oct 2023`|Default is `Etc/UTC`
|
||||
|
||||
The last four examples are not fully qualified. Without a time zone offset, the time zone is set to Etc/UTC (Coordinated Universal Time).
|
||||
The last three examples are not fully qualified, and default to the `Etc/UTC` time zone.
|
||||
|
Reference in New Issue
Block a user