HUGO
Menu
GitHub 90426 stars Mastodon

time.AsTime

Returns the given string representation of a date/time value as a time.Time value.
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:

FormatTime zone
2023-10-15T13:18:50-07:00America/Los_Angeles
2023-10-15T13:18:50-0700America/Los_Angeles
2023-10-15T13:18:50ZEtc/UTC
2023-10-15T13:18:50Default is Etc/UTC
2023-10-15Default is Etc/UTC
15 Oct 2023Default 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:

  1. The TIMEZONE argument
  2. The timeZone setting in your project configuration
  3. The Etc/UTC time 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)