| 1 |
--- |
| 2 |
title: Format |
| 3 |
description: Returns a textual representation of the time.Time value formatted according to the layout string. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: string |
| 9 |
signatures: [TIME.Format LAYOUT] |
| 10 |
aliases: [/methods/time/format] |
| 11 |
--- |
| 12 |
|
| 13 |
```go-template |
| 14 |
{{ $t := "2023-01-27T23:44:58-08:00" }} |
| 15 |
{{ $t = time.AsTime $t }} |
| 16 |
{{ $format := "2 Jan 2006" }} |
| 17 |
|
| 18 |
{{ $t.Format $format }} → 27 Jan 2023 |
| 19 |
``` |
| 20 |
|
| 21 |
> [!NOTE] |
| 22 |
> To [localize](g) the return value, use the [`time.Format`][] function instead. |
| 23 |
|
| 24 |
Use the `Format` method with any `time.Time` value, including the four predefined front matter dates: |
| 25 |
|
| 26 |
```go-html-template |
| 27 |
{{ $format := "2 Jan 2006" }} |
| 28 |
|
| 29 |
{{ .Date.Format $format }} |
| 30 |
{{ .PublishDate.Format $format }} |
| 31 |
{{ .ExpiryDate.Format $format }} |
| 32 |
{{ .Lastmod.Format $format }} |
| 33 |
``` |
| 34 |
|
| 35 |
> [!NOTE] |
| 36 |
> Use the [`time.Format`][] function to format string representations of dates, and to format raw TOML dates that exclude time and time zone offset. |
| 37 |
|
| 38 |
## Layout string |
| 39 |
|
| 40 |
{{% include "/_common/time-layout-string.md" %}} |
| 41 |
|
| 42 |
## Examples |
| 43 |
|
| 44 |
Given this front matter: |
| 45 |
|
| 46 |
{{< code-toggle fm=true >}} |
| 47 |
title = "About time" |
| 48 |
date = 2023-01-27T23:44:58-08:00 |
| 49 |
{{< /code-toggle >}} |
| 50 |
|
| 51 |
The examples below were rendered in the `America/Los_Angeles` time zone: |
| 52 |
|
| 53 |
Format string|Result |
| 54 |
:--|:-- |
| 55 |
`Monday, January 2, 2006`|`Friday, January 27, 2023` |
| 56 |
`Mon Jan 2 2006`|`Fri Jan 27 2023` |
| 57 |
`January 2006`|`January 2023` |
| 58 |
`2006-01-02`|`2023-01-27` |
| 59 |
`Monday`|`Friday` |
| 60 |
`02 Jan 06 15:04 MST`|`27 Jan 23 23:44 PST` |
| 61 |
`Mon, 02 Jan 2006 15:04:05 MST`|`Fri, 27 Jan 2023 23:44:58 PST` |
| 62 |
`Mon, 02 Jan 2006 15:04:05 -0700`|`Fri, 27 Jan 2023 23:44:58 -0800` |
| 63 |
|
| 64 |
## UTC and local time |
| 65 |
|
| 66 |
Convert and format any `time.Time` value to either Coordinated Universal Time (UTC) or local time. |
| 67 |
|
| 68 |
```go-html-template |
| 69 |
{{ $t := "2023-01-27T23:44:58-08:00" }} |
| 70 |
{{ $t = time.AsTime $t }} |
| 71 |
{{ $format := "2 Jan 2006 3:04:05 PM MST" }} |
| 72 |
|
| 73 |
{{ $t.UTC.Format $format }} → 28 Jan 2023 7:44:58 AM UTC |
| 74 |
{{ $t.Local.Format $format }} → 27 Jan 2023 11:44:58 PM PST |
| 75 |
``` |
| 76 |
|
| 77 |
## Ordinal representation |
| 78 |
|
| 79 |
Use the [`humanize`][] function to render the day of the month as an ordinal number: |
| 80 |
|
| 81 |
```go-html-template |
| 82 |
{{ $t := "2023-01-27T23:44:58-08:00" }} |
| 83 |
{{ $t = time.AsTime $t }} |
| 84 |
|
| 85 |
{{ humanize $t.Day }} of {{ $t.Format "January 2006" }} → 27th of January 2023 |
| 86 |
``` |
| 87 |
|
| 88 |
[`humanize`]: /functions/inflect/humanize/ |
| 89 |
[`time.Format`]: /functions/time/format/ |