| 1 |
--- |
| 2 |
title: time.Format |
| 3 |
description: Returns the given date/time as a formatted and localized string. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
aliases: [dateFormat] |
| 9 |
returnType: string |
| 10 |
signatures: [time.Format LAYOUT INPUT] |
| 11 |
aliases: [/functions/dateformat] |
| 12 |
--- |
| 13 |
|
| 14 |
Use the `time.Format` function with `time.Time` values: |
| 15 |
|
| 16 |
```go-html-template |
| 17 |
{{ $t := time.AsTime "2023-10-15T13:18:50-07:00" }} |
| 18 |
{{ time.Format "2 Jan 2006" $t }} → 15 Oct 2023 |
| 19 |
``` |
| 20 |
|
| 21 |
Or use `time.Format` with a parsable string representation of a date/time value: |
| 22 |
|
| 23 |
```go-html-template |
| 24 |
{{ $t := "15 Oct 2023" }} |
| 25 |
{{ time.Format "January 2, 2006" $t }} → October 15, 2023 |
| 26 |
``` |
| 27 |
|
| 28 |
Examples of parsable string representations: |
| 29 |
|
| 30 |
{{% include "/_common/parsable-date-time-strings.md" %}} |
| 31 |
|
| 32 |
To override the default time zone, set the [`timeZone`][] in your project configuration. The order of precedence for determining the time zone is: |
| 33 |
|
| 34 |
1. The time zone offset in the date/time string |
| 35 |
1. The time zone specified in your project configuration |
| 36 |
1. The `Etc/UTC` time zone |
| 37 |
|
| 38 |
## Layout string |
| 39 |
|
| 40 |
{{% include "/_common/time-layout-string.md" %}} |
| 41 |
|
| 42 |
## Localization |
| 43 |
|
| 44 |
Use the `time.Format` function to localize `time.Time` values for the current language and region. |
| 45 |
|
| 46 |
{{% include "/_common/functions/locales.md" %}} |
| 47 |
|
| 48 |
Use the layout string as described above, or one of the tokens below. For example: |
| 49 |
|
| 50 |
```go-html-template |
| 51 |
{{ .Date | time.Format ":date_medium" }} → Jan 27, 2023 |
| 52 |
``` |
| 53 |
|
| 54 |
Localized to en-US: |
| 55 |
|
| 56 |
Token|Result |
| 57 |
:--|:-- |
| 58 |
`:date_full`|`Friday, January 27, 2023` |
| 59 |
`:date_long`|`January 27, 2023` |
| 60 |
`:date_medium`|`Jan 27, 2023` |
| 61 |
`:date_short`|`1/27/23` |
| 62 |
`:time_full`|`11:44:58 pm Pacific Standard Time` |
| 63 |
`:time_long`|`11:44:58 pm PST` |
| 64 |
`:time_medium`|`11:44:58 pm` |
| 65 |
`:time_short`|`11:44 pm` |
| 66 |
|
| 67 |
Localized to de-DE: |
| 68 |
|
| 69 |
Token|Result |
| 70 |
:--|:-- |
| 71 |
`:date_full`|`Freitag, 27. Januar 2023` |
| 72 |
`:date_long`|`27. Januar 2023` |
| 73 |
`:date_medium`|`27.01.2023` |
| 74 |
`:date_short`|`27.01.23` |
| 75 |
`:time_full`|`23:44:58 Nordamerikanische Westküsten-Normalzeit` |
| 76 |
`:time_long`|`23:44:58 PST` |
| 77 |
`:time_medium`|`23:44:58` |
| 78 |
`:time_short`|`23:44` |
| 79 |
|
| 80 |
[`timeZone`]: /configuration/all/#timezone |