mirror of
https://github.com/gohugoio/hugo.git
synced 2025-08-20 21:31:32 +02:00
@@ -1,61 +0,0 @@
|
||||
---
|
||||
title: time.AsTime
|
||||
description: Returns the given string representation of a date/time value as a time.Time value.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [time]
|
||||
related:
|
||||
- functions/time/Duration
|
||||
- functions/time/Format
|
||||
- functions/time/Now
|
||||
- functions/time/ParseDuration
|
||||
returnType: time.Time
|
||||
signatures: ['time.AsTime INPUT [TIMEZONE]']
|
||||
aliases: [/functions/time]
|
||||
toc: true
|
||||
---
|
||||
|
||||
## Overview
|
||||
|
||||
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)
|
||||
```
|
||||
|
||||
## Parsable strings
|
||||
|
||||
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
|
||||
|
||||
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:
|
||||
|
||||
- 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 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
|
||||
3. The time zone specified in your site configuration
|
||||
|
||||
The list of valid time zones may be system dependent, but should include `UTC`, `Local`, or any location in the [IANA Time Zone database].
|
||||
|
||||
[`time.Time`]: https://pkg.go.dev/time#Time
|
||||
[functions]: /functions/time/
|
||||
[iana time zone database]: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
|
||||
[methods]: /methods/time/
|
@@ -1,46 +0,0 @@
|
||||
---
|
||||
title: time.Duration
|
||||
description: Returns a time.Duration value using the given time unit and number.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [duration]
|
||||
related:
|
||||
- functions/time/AsTime
|
||||
- functions/time/Format
|
||||
- functions/time/Now
|
||||
- functions/time/ParseDuration
|
||||
returnType: time.Duration
|
||||
signatures: [time.Duration TIME_UNIT NUMBER]
|
||||
aliases: [/functions/duration]
|
||||
---
|
||||
|
||||
The `time.Duration` function returns a [`time.Duration`] value that you can use with any of the `Duration` [methods].
|
||||
|
||||
This template:
|
||||
|
||||
```go-html-template
|
||||
{{ $duration := time.Duration "hour" 24 }}
|
||||
{{ printf "There are %.0f seconds in one day." $duration.Seconds }}
|
||||
```
|
||||
|
||||
Is rendered to:
|
||||
|
||||
```text
|
||||
There are 86400 seconds in one day.
|
||||
```
|
||||
|
||||
The time unit must be one of the following:
|
||||
|
||||
|
||||
Duration|Valid time units
|
||||
:--|:--
|
||||
hours|`hour`, `h`
|
||||
minutes|`minute`, `m`
|
||||
seconds|`second`, `s`
|
||||
milliseconds|`millisecond`, `ms`
|
||||
microseconds|`microsecond`, `us`, `µs`
|
||||
nanoseconds|`nanosecond`, `ns`
|
||||
|
||||
[`time.Duration`]: https://pkg.go.dev/time#Duration
|
||||
[methods]: /methods/duration
|
@@ -1,77 +0,0 @@
|
||||
---
|
||||
title: time.Format
|
||||
description: Returns the given date/time as a formatted and localized string.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [dateFormat]
|
||||
related:
|
||||
- functions/time/AsTime
|
||||
- functions/time/Duration
|
||||
- functions/time/Now
|
||||
- functions/time/ParseDuration
|
||||
returnType: string
|
||||
signatures: [time.Format LAYOUT INPUT]
|
||||
aliases: [/functions/dateformat]
|
||||
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
|
||||
```
|
||||
|
||||
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
|
||||
```
|
||||
|
||||
Examples of parsable string representations:
|
||||
|
||||
{{% include "functions/time/_common/parsable-date-time-strings.md" %}}
|
||||
|
||||
## Layout string
|
||||
|
||||
{{% include "functions/_common/time-layout-string.md" %}}
|
||||
|
||||
## Localization
|
||||
|
||||
Use the `time.Format` function to localize `time.Time` values for the current language and region.
|
||||
|
||||
{{% include "functions/_common/locales.md" %}}
|
||||
|
||||
Use the layout string as described above, or one of the tokens below. For example:
|
||||
|
||||
```go-html-template
|
||||
{{ .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`
|
@@ -1,48 +0,0 @@
|
||||
---
|
||||
title: time.Now
|
||||
description: Returns the current local time.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: [now]
|
||||
related:
|
||||
- functions/time/AsTime
|
||||
- functions/time/Duration
|
||||
- functions/time/Format
|
||||
- functions/time/ParseDuration
|
||||
returnType: time.Time
|
||||
signatures: [time.Now]
|
||||
aliases: [/functions/now]
|
||||
---
|
||||
|
||||
For example, when building a site on October 15, 2023 in the America/Los_Angeles time zone:
|
||||
|
||||
```go-html-template
|
||||
{{ time.Now }}
|
||||
```
|
||||
|
||||
This produces a `time.Time` value, with a string representation such as:
|
||||
|
||||
```text
|
||||
2023-10-15 12:59:28.337140706 -0700 PDT m=+0.041752605
|
||||
```
|
||||
|
||||
To format and [localize] the value, pass it through the [`time.Format`] function:
|
||||
|
||||
```go-html-template
|
||||
{{ time.Now | time.Format "Jan 2006" }} → Oct 2023
|
||||
```
|
||||
|
||||
The `time.Now` function returns a `time.Time` value, so you can chain any of the [time methods] to the resulting value. For example:
|
||||
|
||||
|
||||
```go-html-template
|
||||
{{ time.Now.Year }} → 2023 (int)
|
||||
{{ time.Now.Weekday.String }} → Sunday
|
||||
{{ time.Now.Month.String }} → October
|
||||
{{ time.Now.Unix }} → 1697400955 (int64)
|
||||
```
|
||||
|
||||
[`time.Format`]: /functions/time/format
|
||||
[localize]: /getting-started/glossary/#localization
|
||||
[time methods]: /methods/time/
|
@@ -1,37 +0,0 @@
|
||||
---
|
||||
title: time.ParseDuration
|
||||
description: Returns a time.Duration value by parsing the given duration string.
|
||||
categories: []
|
||||
keywords: []
|
||||
action:
|
||||
aliases: []
|
||||
related:
|
||||
- functions/time/AsTime
|
||||
- functions/time/Duration
|
||||
- functions/time/Format
|
||||
- functions/time/Now
|
||||
returnType: time.Duration
|
||||
signatures: [time.ParseDuration DURATION]
|
||||
aliases: [/functions/time.parseduration]
|
||||
---
|
||||
|
||||
The `time.ParseDuration` function returns a time.Duration value that you can use with any of the `Duration` [methods].
|
||||
|
||||
|
||||
A duration string is a possibly signed sequence of decimal numbers, each with optional fraction and a unit suffix, such as `300ms`, `-1.5h` or `2h45m`. Valid time units are `ns`, `us` (or `µs`), `ms`, `s`, `m`, `h`.
|
||||
|
||||
This template:
|
||||
|
||||
```go-html-template
|
||||
{{ $duration := time.ParseDuration "24h" }}
|
||||
{{ printf "There are %.0f seconds in one day." $duration.Seconds }}
|
||||
```
|
||||
|
||||
Is rendered to:
|
||||
|
||||
```text
|
||||
There are 86400 seconds in one day.
|
||||
```
|
||||
|
||||
[`time.Duration`]: https://pkg.go.dev/time#Duration
|
||||
[methods]: /methods/duration
|
@@ -1,13 +0,0 @@
|
||||
---
|
||||
cascade:
|
||||
_build:
|
||||
list: never
|
||||
publishResources: false
|
||||
render: never
|
||||
---
|
||||
|
||||
<!--
|
||||
Files within this headless branch bundle are markdown snippets. Each file must contain front matter delimiters, though front matter fields are not required.
|
||||
|
||||
Include the rendered content using the "include" shortcode.
|
||||
-->
|
@@ -1,14 +0,0 @@
|
||||
---
|
||||
# Do not remove front matter.
|
||||
---
|
||||
|
||||
String representation|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
|
||||
|
||||
The last four examples are not fully qualified. Without a time zone offset, the time zone is set to Etc/UTC (Coordinated Universal Time).
|
@@ -1,12 +0,0 @@
|
||||
---
|
||||
title: Time functions
|
||||
linkTitle: time
|
||||
description: Template functions to work with time values.
|
||||
categories: []
|
||||
keywords: []
|
||||
menu:
|
||||
docs:
|
||||
parent: functions
|
||||
---
|
||||
|
||||
Use these functions to work with time values.
|
Reference in New Issue
Block a user