I’ve created a simple Hugo module to easily produce valid <time>
elements, so you can, y’know, add dates to your pages. It takes this:
{{ partial "time" (dict "Time" .PublishDate "Format" "2 January 2006 3:04 PM MST") }}
and produces this:
<time datetime="2023-01-30T00:00:00+1100">30 January 2023 12:00 AM AEDT</time>
The partial also has a couple of other features. It can produce ordinal dates simply by using 2nd
in the time format string, and it can also add in GitHub’s <relative-time>
element:
<time datetime="2023-01-30T00:00:00+1100">
<relative-time datetime="2023-01-30T00:00:00+1100" day="numeric" month="long" year="numeric" hour="numeric" minute="2-digit" timezonename="short">
30th January 2023 12:00 AM AEDT
</relative-time>
</time>
…In case you’re wondering about the 6 different attributes added to the <relative-time>
element, currently the partial will parse the time format string and set the appropriate attributes so the formatted date produced by the <relative-time>
element matches the formatted date produced by Go. This may change in the future.
In fact, quite a few things might change. I’m still trying to figure out how I can improve some areas, so I might make some adjustments here and there. If anybody here wants to kick the tyres on this module, you can install it from github.com/kcastellino/hugo-mod-time.