time.AsTime
- Syntax
- time.AsTime INPUT [TIMEZONE]
- Returns
- time.Time
- Alias
- time
Usage
The time.AsTime function returns the given date/time as a time.Time value. Hugo provides functions and methods to format, localize, compare, and manipulate time.Time values. Use the time.AsTime function to convert a date/time value before passing it to one of these functions or methods.
Hugo converts the four predefined front matter dates to time.Time values, but not custom dates, which you specify as custom page parameters under the params key in front matter. A custom date is a time.Time value only if it is an unquoted TOML date with a time zone offset. Use the time.AsTime function to convert any other custom date to a time.Time value.
A string INPUT must be a parsable representation of a date/time value, such as one of these:
| Format | Time zone offset |
|---|---|
2023-10-15T13:18:50-07:00 | -07:00 |
2023-10-15T13:18:50-0700 | -07:00 |
2023-10-15T13:18:50Z | +00:00 |
2023-10-15T13:18:50 | None |
2023-10-15 | None |
15 Oct 2023 | None |
The last three examples do not include a time zone offset.
If the INPUT includes a time zone offset, the function uses that offset. Otherwise, the function assigns a time zone, which determines the offset for that date, in this order of precedence:
- The
TIMEZONEargument - The
timeZonesetting in your project configuration - The
Etc/UTCtime zone
Arguments
INPUT- (
stringortime.Time) The date/time value, such as a custom date from JSON, TOML, or YAML front matter, quoted or unquoted. A string must be a parsable representation of a date/time value. If theINPUTis atime.Timevalue, the function returns the same date/time. TIMEZONE- (
string) Optional. The time zone to use when theINPUTdoes not include a time zone offset. UseUTC,Localfor the system’s local time zone, or any location in the IANA Time Zone database. The list of valid time zones may depend on the system. The function ignores this argument if theINPUTis atime.Timevalue. To represent atime.Timevalue in a different time zone, use thetime.Infunction.
Examples
{{ time.AsTime "2023-10-15T13:18:50-07:00" }} → 2023-10-15 13:18:50 -0700 -0700 (time.Time)Setting the time zone of a string without a time zone offset:
{{ time.AsTime "15 Oct 2023" "America/Los_Angeles" }} → 2023-10-15 00:00:00 -0700 PDT (time.Time)Converting a custom date in YAML front matter, such as an eventDate parameter set to 2023-10-15 under the params key, and calling a method on the returned value:
{{ with .Params.eventDate }}
{{ (time.AsTime .).Year }} → 2023 (int)
{{ end }}