diff options
author | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-04-28 11:52:15 +0200 |
---|---|---|
committer | Bjørn Erik Pedersen <bjorn.erik.pedersen@gmail.com> | 2022-04-28 11:52:15 +0200 |
commit | 4852a3765343c5df5c147b54b8e7ab45d1d9ed33 (patch) | |
tree | 469c8c3e0373537159d182a6dec872744558cbaa /docs/content/en/functions | |
parent | fa80fe3c8ab523846178f94fdc65c997d8eef10c (diff) | |
parent | 3902f9a4767fe6e62ac5146728d8311b8cd227e0 (diff) |
Merge commit '3902f9a4767fe6e62ac5146728d8311b8cd227e0'
Diffstat (limited to 'docs/content/en/functions')
-rw-r--r-- | docs/content/en/functions/adddate.md | 58 | ||||
-rw-r--r-- | docs/content/en/functions/relLangURL.md | 4 |
2 files changed, 28 insertions, 34 deletions
diff --git a/docs/content/en/functions/adddate.md b/docs/content/en/functions/adddate.md index 56008c956..116ffa8de 100644 --- a/docs/content/en/functions/adddate.md +++ b/docs/content/en/functions/adddate.md @@ -1,6 +1,6 @@ --- title: .AddDate -description: Returns the time corresponding to adding the given number of years, months, and days passed to the function. +description: Returns the time corresponding to adding the given number of years, months, and days to the given time.Time value. date: 2017-02-01 publishdate: 2017-02-01 lastmod: 2017-02-01 @@ -17,35 +17,29 @@ deprecated: false aliases: [] --- +```go-html-template +{{ $d := "2022-01-01" | time.AsTime }} -The `AddDate` function takes three arguments in logical order of `years`, `months`, and `days`. - -## Example: Randomized Tweets from the Last 2 Years - -Let's assume you have a file at `data/tweets.toml` that contains a list of Tweets to display on your site's homepage. The file is filled with `[[tweet]]` blocks; e.g.--- - -{{< code-toggle file="data/tweets" >}} -[[tweet]] -name = "Steve Francia" -twitter_handle = "@spf13" -quote = "I'm creator of Hugo. #metadocreference" -link = "https://twitter.com/spf13" -date = "2017-01-07T00:00:00Z" -{{< /code-toggle >}} - -Let's assume you want to grab Tweets from the last two years and present them in a random order. In conjunction with the [`where`](/functions/where/) and [`now`](/functions/now/) functions, you can limit our range to the last two years via `now.AddDate -2 0 0`, which represents a point in time 2 years, 0 months, and 0 days before the time of your last site build. - -{{< code file="partials/templates/random-tweets.html" download="tweets.html" >}} -{{ range where $.Site.Data.tweets.tweet "date" "ge" (now.AddDate -2 0 0) | shuffle }} - <div class="item"> - <blockquote> - <p> - {{ .quote | safeHTML }} - </p> - — {{ .name }} ({{ .twitter_handle }}) <a href="{{ .link }}"> - {{ dateFormat "January 2, 2006" .date }} - </a> - </blockquote> - </div> -{{ end }} -{{< /code >}} +{{ $d.AddDate 0 0 1 | time.Format "2006-01-02" }} --> 2022-01-02 +{{ $d.AddDate 0 1 1 | time.Format "2006-01-02" }} --> 2022-02-02 +{{ $d.AddDate 1 1 1 | time.Format "2006-01-02" }} --> 2023-02-02 + +{{ $d.AddDate -1 -1 -1 | time.Format "2006-01-02" }} --> 2020-11-30 +``` + +{{% note %}} +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. + +See [this explanation](https://github.com/golang/go/issues/31145#issuecomment-479067967) from the Go team. +{{% /note %}} + +```go-html-template +{{ $d := "2023-01-31" | time.AsTime }} +{{ $d.AddDate 0 1 0 | time.Format "2006-01-02" }} --> 2023-03-03 + +{{ $d := "2024-01-31" | time.AsTime }} +{{ $d.AddDate 0 1 0 | time.Format "2006-01-02" }} --> 2024-03-02 + +{{ $d := "2024-02-29" | time.AsTime }} +{{ $d.AddDate 1 0 0 | time.Format "2006-01-02" }} --> 2025-03-01 +``` diff --git a/docs/content/en/functions/relLangURL.md b/docs/content/en/functions/relLangURL.md index e624aa355..5cca28c69 100644 --- a/docs/content/en/functions/relLangURL.md +++ b/docs/content/en/functions/relLangURL.md @@ -17,7 +17,7 @@ deprecated: false aliases: [] --- -`absLangURL` and `relLangURL` functions are similar to their [`absURL`](/functions/absurl/) and [`relURL`](/functions/relurl/) relatives but will add the correct language prefix when the site is configured with more than one language. (See [Configuring Multilingual][multiliconfig].) +`absLangURL` and `relLangURL` functions are similar to their [`absURL`](/functions/absurl/) and [`relURL`](/functions/relurl/) relatives but will add the correct language prefix when the site is configured with more than one language. (See [Configure Languages][multiliconfig].) So for a site `baseURL` set to `https://example.com/hugo/` and the current language is `en`: @@ -26,4 +26,4 @@ So for a site `baseURL` set to `https://example.com/hugo/` and the current lang {{ "blog/" | relLangURL }} → "/hugo/en/blog/" ``` -[multiliconfig]: /content-management/multilingual/#configuring-multilingual-mode +[multiliconfig]: /content-management/multilingual/#configure-languages |