HomeThe ClassicsFarai's Codelab

Ok, How Do I Get These Bloody Dates To Work Properly In Hugo?

Published:

Honestly, this post is more for me than for you so don’t expect to find it useful. If you do then I’ll be shocked. If you want to see what I settled on, jump to the end.

This is how I want dates to work in Hugo:

  • Lastmod for when the post was last modified (which is when I’ve made noticable changes),
  • PublishDate is when a post was published, reposted or the scheduled date of publishing, and
  • Date is when a post was first published.

Here’s what I want to do

  1. I’ve created a new post and I want to publish it immediatley—PublishDate or Date works here.
  2. I want to update the post in 1, so I set a Lastmod date.
  3. I want to republish an old post, so I’d use Date for the inital date the post was published, PublishDate for when the post was republished and maybe a Lastmod for the last time I updated a post.
  4. Some things don’t have publish dates per say since they’re constantly updated. Think the /now page.

This all sounds easy enough but the issue is getting it to show up on a web page.

Let me try a truth table instead:

LastModDatePublishDateWhat to show
TTTPublishDate is Reposted, Date is initially posted, Lastmod is when it was last updated
TTFThrow Error
TFFJust show Lastmod date
FTTPublishDate is Reposted, Date is initially posted
FFTJust show PublishDate
TFTPublishDate is Published, Lastmod is the updated date

Much clearer now, though the issue is how to sort posts in a list.

My Solution for Now

This is what I’ve settled on:

  • PublishDate is the date a post was actually published/republished,
  • LastMod is when the post was last modified
  • InitialDate is when the post was initially posted
  • Date is the post’s date in order of Date, PublishDate and Lastmod

I then add this to my config file:

frontmatter:
  publishdate:
    - publishdate
    - date
  date:
    - date
    - publishdate
    - lastmod
  lastmod:
    - lastmod

Using this seems to work for now. I’ll see how it works as I go along.