Skip to content

Commit

Permalink
Allow override of date format (#291)
Browse files Browse the repository at this point in the history
Previously the default date format (`January 2, 2006` ) was used for
blog and single page date visualizations.

The new parameter `date_format` can be used to override this format.
This allows the visualization of localized dates or a changed precision
(down to the minute or up to the month or year of publication).

Co-authored-by: Regis Philibert <login@regisphilibert.com>
  • Loading branch information
sumpfralle and regisphilibert committed Oct 1, 2021
1 parent 7752770 commit b0bf3cc
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 2 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,18 @@ You can replace the title of your site in the top left corner of each page with
```


### Localize date format

Dates of blog posts and single pages are rendered with the default date format commonly used in the USA and Canada. It is possible to specify a different format.

```
[params]
date_format = "2. January 2006"
```

See hugo's documentation of the [`dateFormat` function](https://gohugo.io/functions/dateformat/) for more details.


### Nearly finished

In order to see your site in action, run Hugo's built-in local server.
Expand Down
2 changes: 1 addition & 1 deletion layouts/_default/single.html
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ <h1 class="f1 athelas mt3 mb1">
{{/* Hugo uses Go's date formatting is set by example. Here are two formats */}}
{{ if not .Date.IsZero }}
<time class="f6 mv4 dib tracked" {{ printf `datetime="%s"` (.Date.Format "2006-01-02T15:04:05Z07:00") | safeHTMLAttr }}>
{{- .Date.Format "January 2, 2006" -}}
{{- .Date.Format (default "January 2, 2006" .Site.Params.date_format) -}}
</time>
{{end}}

Expand Down
2 changes: 1 addition & 1 deletion layouts/post/summary.html
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<div class="mb3 pa4 mid-gray overflow-hidden">
{{ if .Date }}
<div class="f6">
{{ .Date.Format "January 2, 2006" }}
{{ .Date.Format (default "January 2, 2006" .Site.Params.date_format) }}
</div>
{{ end }}
<h1 class="f3 near-black">
Expand Down

0 comments on commit b0bf3cc

Please sign in to comment.