How to format .Date with 'YY-MM-DD HH:mm'?

In template, I use {{ .Lastmod.Format "2006-01-02 11:11" }} to format the lastmod value.

But the result is strange. e.g.

+++
date = 2018-04-20T21:41:23+08:00
lastmod = 2018-04-23T21:41:23+08:00
+++

The template:

{{ .Lastmod.Format "2006-01-02 11:11" }}
or
{{ dateFormat "2006-01-02 11:11" .Lastmod }}

What I want is 2018-04-23 21:41, but the result is 2018-04-23 21:44? What’s the extra 3 minutes?

How can I do that?

And if the lastmod value in the front matter can be automatically updated when I modify the file content?

Try {{ dateFormat “2006-01-02 11:11” .Lastmod }}

According to the docs, the .lastmod is updated using git data, when enabled: https://gohugo.io/variables/git/#lastmod

There is also a flexible new “dates in frontmatter” config that @kaushalmodi has promised to get documented :slight_smile: Git is one of the options, but you could also configure Hugo to use the file lastmod timestamp … That is, however, not very accurate if pull from Git all the time.

Not sure if I follow :slight_smile:

If you are talking about this:

You had already documented that in 0dbf79c2, and there’s some related discussion in #4494 too.

1 Like

Also not clear on what “result is failed” means… the date format doesn’t look like the standard date format.

It should be {{ .Lastmod.Format "2006-01-02 03:04PM" }} or {{ .Lastmod.Format "2006-01-02 15:04" }}.

Ref:

Here’s how I remember: 1, 2, 3, 4, 5, 6 → month, day, hour, min, sec, year

2 Likes

I forgot …

Maybe the default configuration lastmod = [":git" "lastmod", "date","publishDate"] is nearly what I want.

I added enableGitInfo = true in the config file and deleted the lastmod value in the content file front matter.

The format result is 2018-04-21 09:44 based on the git author date Sat, Apr 21, 2018, 9:30 AM using {{ .Lastmod.Format "2006-01-02 15:11" }}.

The git author date seem not to be fetched timely by Hugo every time I commit the content file on development. Because after I commit the file several time the lastmod keeps value with 2018-04-21 09:44

After I rebuild the site, the lastmod vaule finally update to 2018-04-21 10:44 based on the git author date Sat, Apr 21, 2018, 10:07 AM. Obviously the time does not match. I doubt that whether the format 2006-01-02 15:11 is really works as expected.

A post was split to a new topic: Format date as YY-MM-DD HH:mm