time.AsTime
Syntax
time.AsTime INPUT [TIMEZONE]
Returns
time.Time
Alias
time
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.
{{ $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:
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).
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{{ time.AsTime "15 Oct 2023" "America/Chicago" }}
-
Set the default time zone in your site configuration
hugo.timeZone: America/New_York
timeZone = 'America/New_York'
{ "timeZone": "America/New_York" }
The order of precedence for determining the time zone is:
- The time zone offset in the date/time string
- The time zone provide as the second argument to the
time.AsTime
function - 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.