Add a Last Edited Date to Posts
With frequently changing information it can be useful to record both when a post was created and when it was last updated - showing that it’s been reviewed recently. We do this on this site by showing the created date and showing the last modified date if it’s different from the created date (see above the title).
There’s two ways of doing this in Hugo:
- recording when you last changed it
- using the last changed date in Git (if you use it)
(or a blend of the two)
The Manual Way:
The simplest way is to record a
lastmod date and time within your post’s header - like the example below:
We can then use this information in our theme and layouts to display the information. In our code, we don’t show the last updated date if it’s the same as the created date (there’s no point?)
You can of course change the formatting to be however you would like. We do have a post on showing it with ordinals like ‘th’ and ‘st’ if it’s useful.
The Git way
Hugo can actually hook into your git (the version control system) information and pull the last edited times from there. To enable it, just change this setting in your config.
This will now automatically pull in the last updated times and fill in
lastmod for you - Neat!
It will, however, overwrite any of your manually created
lastmod dates. You can choose to change this behaviour to favour your
lastmod times but if they don’t exist then use git. To do this, we need to add one final part to our config changing the front matter dates:
That’s all there is to it - good luck! Let us know how you get on in the comments.
Site illustrations (of Hugo the rabbit) drawn by Carina Roberts.