Hello,
For few posts, I would like to have a future publishdate. But I would also like to retain the ‘date’ in the frontmatter that shows the date when I started working on that blog post.
But when the post gets published, I would like to publish the post date as the ‘publishdate’ instead of ‘date’. While looking around for this option, I came across this open issue: https://github.com/spf13/hugo/issues/2145
So while this feature is added, I tried to implement that in my template (for example, in _default/simple.html):
<span class="post-date">
{{ with .PublishDate }}
{{ $.PublishDate.Format $.Site.Params.DateForm }}
{{ else }}
{{ $.Date.Format $.Site.Params.DateForm }}
{{ end }}
</span>
But that does work as the default value of .PublishDate is always set, to Jan 1 0001. So my post-date will always be Jan 1 0001, unless I explicitly set the publishdate.
How can I ignore the publishdate unless its value is not “Jan 1 0001”?
Can anyone share their template where they print the publishdate if a publishdate is set in the frontmatter, and print the date otherwise?
Thanks.
I found an answer, which I am posting here. Please advise if there’s a better way to do this at the present.
I created this partial called publishdate-maybe
:
{{ with .PublishDate }}
{{ if eq ($.PublishDate.Format "2006-01-02") "0001-01-01" }}
<!-- Print the Date instead of PublishDate if PublishDate is defined but at its initial value of Jan 1, 0001 -->
{{ $.Date.Format $.Site.Params.DateForm }}
{{ else }}
{{ $.PublishDate.Format $.Site.Params.DateForm }}
{{ end }}
{{ else }}
{{ $.Date.Format $.Site.Params.DateForm }}
{{ end }}
2 Likes
Thanks for sharing! I was wrestling with a good way to display the original publish date in my post bylines while falling back to date
when no publishdate
is set in post front matter and this worked great. Here’s how I’m using it along with the time
element to produce machine- and human-readable dates.
{{ with .PublishDate }}
{{ if .IsZero }}
<time datetime="{{ $.Date.Format "2006-01-02T15:04:05-07:00" }}">{{ $.Date.Format "2 Jan, 2006" }}</time>
{{ else }}
<time datetime="{{ .Format "2006-01-02T15:04:05-07:00" }}">{{ .Format "2 Jan, 2006" }}</time>
{{ end }}
{{ end }}
The example uses $.PublishDate.IsZero
instead of doing an equality check on a formatted date string. It also removes the branch condition where .PublishDate
would be falsy, as I was never able to hit that condition in Hugo v0.18.
This can then further be simplified like:
{{ if .PublishDate.IsZero }}
<time datetime="{{ .Date.Format "2006-01-02T15:04:05-07:00" }}">{{ .Date.Format "2 Jan, 2006" }}</time>
{{ else }}
<time datetime="{{ .PublishDate.Format "2006-01-02T15:04:05-07:00" }}">{{ .PublishDate.Format "2 Jan, 2006" }}</time>
{{ end }}
Which can be further simplified using default
as:
{{ $date := default .Date (.PublishDate) }}
<time datetime="{{ $date.Format "2006-01-02T15:04:05-07:00" }}">{{ $date.Format "2 Jan, 2006" }}</time>
Then be reduced using the dateFormat
function:
<time datetime="{{ dateFormat "2006-01-02T15:04:05-07:00" (default .Date (.PublishDate)) }}">
{{ dateFormat "2 Jan, 2006" (default .Date (.PublishDate)) }}
</time>
And applied to the original use case, resulting in:
{{ dateFormat .Site.Params.DateForm (default .Date (.PublishDate)) }}
```
2 Likes
I am glad that helped. Out of curiosity, what syntax threw errors on hugo v0.18. I am running it fine on the dev version.
I failed to mention that for $.Site.Params.DateForm
to work, you need to define the [Params]
> DateForm
parameter in the config.toml
.
2 Likes
Out of curiosity, what syntax threw errors on hugo v0.18.
My bad. Looking at your example I don’t see any issues. The problem I was having was related to scoping, likely caused when I was moving my time
elements inside the brach logic. Either way the snippet you provided saved my day. Thanks again!
1 Like
Thanks. I did not know of that function.
I didn’t hit that branch condition either. But looks like it is safe to assume that .PublishDate
will always exist even when user hasn’t defined it.
Thank you! I am now stealing that!
… Done
1 Like