Add current date and time to Hugo page front matter

Hi there

is there a way to add something like this in the Hugo front matter

lastmod: {{ now.date}}

instead of

lastmod: 2020-06-17T14:23:39+01:00

as it will be great if the page can update this every time I modify and save a .md file

currently I do it manually which I forgot all the time and also it is extra work to do for no reason if Hugo can do it for me using those brackets things of gotemplate

thanks

You need to use a Hugo Archetype for this and execute the command hugo new <path-to-file>

Instead of defining lastmod in front matter, use the git author date or the file modification time when displaying .Lastmod on a page.

See:

With this site configuration…

enableGitInfo = true

[frontmatter]
lastmod = [':git', ':fileModTime']

…the .Lastmod page method will first try to retrieve the git author date. If that fails, it will look at the file modification time (mtime).

3 Likes

it took me some time to understand this but it works like magic thanks alot

I am sorry I did not understand what you mean by Hugo new

I think this creates a new page

from what I understand archetype is used when I create a Hugo page but can I update the existing page using archetype to update the modification date?

thanks

Archetypes are used with the command hugo new to create a new content file.
You cannot update the modification date with that command.
Use J Mooring’s solution for automatic lastmod updates.

2 Likes

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