Add a Last Modified Date in Hugo Articles

  • vnull
  • Pub Dec 29, 2022
  • Edited Dec 30, 2022
  • 3 minutes read

Add a Last Modified Date in Hugo Articles

Add date data to our Hugo posts

  • Last Modified
  • Last Updated
  • Edited

Conditionally render a Last Modified date in the single.html template for our posts.

Published {{ .Date.UTC.Format "Jan 2, 2006" }}
Last Modified {{ .Lastmod.UTC.Format "Jan 2, 2006" }}

Use Hugo’s date and lastmod fields.

Info
With multiple timezones need to normalize them by converting them all to UTC or all to our local timezone using UTC and Local.

Two ways we go about this:

1. Automatically add lastmod field using Git metadata

Automatically add the lastmod field to each article based on Git information.

Update enableGitInfo in config.toml

Set enableGitInfo to true in our config.toml.

enableGitInfo = true

The lastmod field will default to the most recent commit date for that file in Git.

Update frontmatter in config.toml

If enableGitInfo is true, it will override any lastmod dates in front matter.

By default, :git comes before lastmod. To override add the following lines to the config file to config.toml

enableGitInfo = true

[frontmatter]
date = ["date", "publishDate", "lastmod"]
lastmod = ["lastmod", ":git", "date", "publishDate"]
publishDate = ["publishDate", "date"]
expiryDate = ["expiryDate"]
Note
This approach is not recommended, since changes to a Git file may not always correlate to changes in the article content. It could simply be a minor style change or other changes that aren’t relevant to the content.

2. Manually add lastmod to front matter

Second approach is to manually add lastmod fields to front matter whenever an article is updated.

We want the Last Modified date to show only if we explicitly set it in the front matter.

Add lastmod to front matter

Add the lastmod field to our article front matter:

date: "2022-01-10"
lastmod: "2022-01-15"
Info

If enableGitInfo is true, lastmod will be set to the most recent commit date for that file and override any lastmod dates.

Cange this behavior by configuring front matter dates: ["lastmod", "date", "publishDate"]

Created a simple partial template

This partial template checks if the lastmod and publish dates are the same then renders the dates. Compare two dates, Jan 2, 2006 is equal to mm.dd.yyyy.

Create lastmod.html

{{ $date := .Date.UTC.Format "Jan 2, 2006" }}
{{ $lastmod := .Lastmod.UTC.Format "Jan 2, 2006" }}

<li class="list-inline-item d-flex align-items-center">
    <i class="fas fa-calendar me-2"></i>
    <span>Published on {{ $date }}</span>

<li class="list-inline-item d-flex align-items-center">
    <i class="fas fa-calendar me-2"></i>
    {{ if ne $lastmod $date }}
    <span> Edited on {{ $lastmod }}</span>
    {{ end }}
</li>

In single.html or post.html this can be called with the following {{ partial "lastmod.html" . }}

Summary

Minor update to improve information.

Check out the something interesting:

Do you have an idea or suggestion for a blog post? Submit it here!

Related Posts

2023 Phoenix VMUG UserCon

  • vnull
  • Sep 8, 2023
  • 4 minutes read

Introduction: The recent 2023 Phoenix VMUG UserCon brought together some like-minded people in the field, with discussions ranging from VMware technologies to best practices for optimizing existing systems.

Read more

Red Hat User Group Insights, Ansible Automation Platform, and ITSM Integration

  • vnull
  • Jun 1, 2023
  • 3 minutes read

Introduction: This blog post aims to summarize the key takeaways from this informative workshop. At the recent Red Hat User Group workshop on Red Hat Insights, Red Hat Ansible Automation Platform, and their integration with management (ITSM) systems, such as ServiceNow, provided valuable insights into how these technologies work together.

Read more

Robocopy Examples

  • vnull
  • Feb 10, 2023
  • 5 minutes read

Robocopy Examples Robocopy has many command line options and it can be overwhelming to know which commands to use. In this post, we will take a look at how to ues robocopy to copy, mirror, purge Files and Folders.

Read more