Conditional with date math

I would like to conditionally include some part of a template at content generation time, only if it is N days past the post date.

Does the Hugo templating system currently support this type of date math and conditional?

I understand that the content will not dynamically show up and that the site will need to be generated again.

Inspired by this question and response:

1 Like

After more experimenting, it seems this works for me:

{{ if (lt (add .Date.Unix 604800) .Now.Unix) }}
  ...thing to hide until 7 days after post date...
{{ end }}

For those who are curious, the “thing” is ads. A lot of this site’s traffic comes from organic searches and referral clickthroughs to old posts. I’m testing ads at the bottom of those posts, but would rather not show ads to the readers who regularly follow the blog and read new content.

2 Likes