| 1 |
--- |
| 2 |
title: time.AsTime |
| 3 |
description: Returns the given string representation of a date/time value as a time.Time value. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [time] |
| 9 |
returnType: time.Time |
| 10 |
signatures: ['time.AsTime INPUT [TIMEZONE]'] |
| 11 |
aliases: [/functions/time] |
| 12 |
--- |
| 13 |
|
| 14 |
## Overview |
| 15 |
|
| 16 |
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. |
| 17 |
|
| 18 |
```go-html-template |
| 19 |
{{ $t := "2023-10-15T13:18:50-07:00" }} |
| 20 |
{{ time.AsTime $t }} → 2023-10-15 13:18:50 -0700 PDT (time.Time) |
| 21 |
``` |
| 22 |
|
| 23 |
## Parsable strings |
| 24 |
|
| 25 |
As shown above, the first argument must be a parsable string representation of a date/time value. For example: |
| 26 |
|
| 27 |
{{% include "/_common/parsable-date-time-strings.md" %}} |
| 28 |
|
| 29 |
To override the default time zone, set the [`timeZone`][] in your project configuration or provide a second argument to the `time.AsTime` function. For example: |
| 30 |
|
| 31 |
```go-html-template |
| 32 |
{{ time.AsTime "15 Oct 2023" "America/Los_Angeles" }} |
| 33 |
``` |
| 34 |
|
| 35 |
The list of valid time zones may be system dependent, but should include `UTC`, `Local`, or any location in the [IANA Time Zone database][]. |
| 36 |
|
| 37 |
The order of precedence for determining the time zone is: |
| 38 |
|
| 39 |
1. The time zone offset in the date/time string |
| 40 |
1. The time zone provided as the second argument to the `time.AsTime` function |
| 41 |
1. The time zone specified in your project configuration |
| 42 |
1. The `Etc/UTC` time zone |
| 43 |
|
| 44 |
[IANA Time Zone database]: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones |
| 45 |
[`time.Time`]: https://pkg.go.dev/time#Time |
| 46 |
[`timeZone`]: /configuration/all/#timezone |
| 47 |
[functions]: /functions/time/ |
| 48 |
[methods]: /methods/time/ |