| 1 |
--- |
| 2 |
title: AddDate |
| 3 |
description: Returns the time corresponding to adding the given number of years, months, and days to the given time.Time value. |
| 4 |
categories: [] |
| 5 |
keywords: [] |
| 6 |
params: |
| 7 |
functions_and_methods: |
| 8 |
returnType: time.Time |
| 9 |
signatures: [TIME.AddDate YEARS MONTHS DAYS] |
| 10 |
aliases: [/functions/adddate] |
| 11 |
--- |
| 12 |
|
| 13 |
```go-html-template |
| 14 |
{{ $d := "2022-01-01" | time.AsTime }} |
| 15 |
|
| 16 |
{{ $d.AddDate 0 0 1 | time.Format "2006-01-02" }} → 2022-01-02 |
| 17 |
{{ $d.AddDate 0 1 1 | time.Format "2006-01-02" }} → 2022-02-02 |
| 18 |
{{ $d.AddDate 1 1 1 | time.Format "2006-01-02" }} → 2023-02-02 |
| 19 |
|
| 20 |
{{ $d.AddDate -1 -1 -1 | time.Format "2006-01-02" }} → 2020-11-30 |
| 21 |
``` |
| 22 |
|
| 23 |
> [!NOTE] |
| 24 |
> When adding months or years, Hugo normalizes the final `time.Time` value if the resulting day does not exist. For example, adding one month to 31 January produces 2 March or 3 March, depending on the year. |
| 25 |
> |
| 26 |
> See [this explanation][] from the Go team. |
| 27 |
|
| 28 |
```go-html-template |
| 29 |
{{ $d := "2023-01-31" | time.AsTime }} |
| 30 |
{{ $d.AddDate 0 1 0 | time.Format "2006-01-02" }} → 2023-03-03 |
| 31 |
|
| 32 |
{{ $d := "2024-01-31" | time.AsTime }} |
| 33 |
{{ $d.AddDate 0 1 0 | time.Format "2006-01-02" }} → 2024-03-02 |
| 34 |
|
| 35 |
{{ $d := "2024-02-29" | time.AsTime }} |
| 36 |
{{ $d.AddDate 1 0 0 | time.Format "2006-01-02" }} → 2025-03-01 |
| 37 |
``` |
| 38 |
|
| 39 |
[this explanation]: https://github.com/golang/go/issues/31145#issuecomment-479067967 |