How to set date format of 2006-01-02 in config.toml


I’m stuck at getting dates to render in the YYYY-MM-DD form. Ideally, I would like to do this globally in config.toml and avoid setting it in templates. Using the magic 2006-01-02 fails.

I’ve tried variations on

    date = ["date", "publishDate", "lastmod"]
    lastmod = [":git", "lastmod", "date", "publishDate"]0
    publishDate = ["publishDate", "date"]
    expiryDate = ["expiryDate"]

all of which yield dates in the form Aug 5, 2020. I’m unsure how and where to use

date = ["myDate", ":default"]

Yihui Xie, avoided the issue in his adaptation of the Hugo-Lithium theme for his R:blogdown package by using markup in his templates:

… span class=“article-date”>{{ .Date.Format “2006-01-02” }} …

in single.html and

{{ .Date.Format “2006-01-02” }}

in list.html.

Is what I’m attempting possible? Pointers gratefully received.

The configuration for frontmatter is not used to configure date formats. This tells Hugo how to “calculate” or assign date values to the various date page variables.

Take this config line for example:

date = ["date", "publishDate", "lastmod"]

This tells Hugo that if you reference {{ .Date }} from within a Page context, it will first look at whether that Page has date specified in its frontmatter. If not, it will look at whether publishDate is specified. If not, then check lastmod value.

^ is exactly how to tell Hugo how to display your various dates.

If you want to make it a site-wide config value, what you could do is add a param in your config:

mydateformat = "2006-01-02"

and use it as

{{ .Date.Format site.Params.mydateformat }}
1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.