time.AsTime
- Syntax
- time.AsTime INPUT [TIMEZONE]
- Returns
- time.Time
- Alias
- time
Usage
The time.AsTime function returns the given string representation of a date/time value 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 string before passing it to one of these functions or methods.
The INPUT must be a parsable string representation of a date/time value, such as one of these:
| Format | Time zone |
|---|---|
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 three examples are not fully qualified, and default to the Etc/UTC time zone.
If the INPUT includes a time zone offset, the function uses that offset. Otherwise, the function determines the time zone in this order of precedence:
- The
TIMEZONEargument - The
timeZonesetting in your project configuration - The
Etc/UTCtime zone
Set the TIMEZONE to UTC, Local, or any location in the IANA Time Zone database. The list of valid time zones may depend on the system.
Examples
{{ time.AsTime "2023-10-15T13:18:50-07:00" }} → 2023-10-15 13:18:50 -0700 PDT (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)Calling a method on the returned value:
{{ (time.AsTime "2023-10-15T13:18:50-07:00").Year }} → 2023 (int)