adddate.md (1430B)
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 date: 2017-02-01 5 publishdate: 2017-02-01 6 lastmod: 2017-02-01 7 categories: [functions] 8 menu: 9 docs: 10 parent: "functions" 11 keywords: [dates,time] 12 signature: [".AddDate YEARS MONTHS DAYS"] 13 workson: [times] 14 hugoversion: 15 relatedfuncs: [now] 16 deprecated: false 17 aliases: [] 18 --- 19 20 ```go-html-template 21 {{ $d := "2022-01-01" | time.AsTime }} 22 23 {{ $d.AddDate 0 0 1 | time.Format "2006-01-02" }} --> 2022-01-02 24 {{ $d.AddDate 0 1 1 | time.Format "2006-01-02" }} --> 2022-02-02 25 {{ $d.AddDate 1 1 1 | time.Format "2006-01-02" }} --> 2023-02-02 26 27 {{ $d.AddDate -1 -1 -1 | time.Format "2006-01-02" }} --> 2020-11-30 28 ``` 29 30 {{% note %}} 31 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. 32 33 See [this explanation](https://github.com/golang/go/issues/31145#issuecomment-479067967) from the Go team. 34 {{% /note %}} 35 36 ```go-html-template 37 {{ $d := "2023-01-31" | time.AsTime }} 38 {{ $d.AddDate 0 1 0 | time.Format "2006-01-02" }} --> 2023-03-03 39 40 {{ $d := "2024-01-31" | time.AsTime }} 41 {{ $d.AddDate 0 1 0 | time.Format "2006-01-02" }} --> 2024-03-02 42 43 {{ $d := "2024-02-29" | time.AsTime }} 44 {{ $d.AddDate 1 0 0 | time.Format "2006-01-02" }} --> 2025-03-01 45 ```