How to use AddDate in Frontmatter?

I like to add the expirydate in frontmatter like this

expirydate = “{{ .Lastmod.AddDate 2 0 0 }}”

tested various things and no solution…
Any suggestions?

Thanks

Template functions cannot work directly in front-matter (or content files for that matter).

They work only in the layout files. (Update: Oops, looks like I made a wrong statement. Sorry for misguidance.)


On a related note, .Add, .Sub, etc. work for dates [see].

1 Like

A few All template functions also work in Hugo archetypes

3 Likes

Yes, I want to have it in archetypes!
How to do it?

my archetype file:

categories  = ["Post"]
tags        = ["{{ now.Format "2006"}}"]
description = "{{ replace .TranslationBaseName "-" " " | title }}"
title       = "{{ replace .TranslationBaseName "-" " " | title }}"
date        = "{{ .Date }}"
#expirydate  = "{{ .Date | time | .AddDate 2 0 0}}"  # no solution!
+++
**Insert Lead paragraph here.**
<!--more-->

See

Correct!
I was looking for a way to set expirydate too!

All template functions. And the full site.

3 Likes

Have you entered the above commented out in your archetype or is this just the way you posted it here?

Also .AddDate takes the following:

.AddDate YEARS MONTHS DAYS

Not sure how .AddDate 2 0 0 is supposed to work.

this was one try of many without success - see the comment in this line!

Did you try the .Add function I suggested earlier?

This

expirydate: {{ (.Date | time).AddDate 2 0 0 }}

generates the following for me:

date: 2018-08-03T15:21:46+10:00
expirydate: 2020-08-03 15:21:46 +1000 AEST

I also tried:

expirydate: {{ now.AddDate 2 0 0 }}

which gave me:

date: 2018-08-03T15:26:06+10:00
expirydate: 2020-08-03 15:26:06.495604604 +1000 AEST
2 Likes

Thanks, this works now for me.
I added in the archetype

expirydate = “{{ ((.Date | time ).AddDate 2 0 0).Format “2006-01-02T15:04:05Z07:00”}}”

Thank you for this contribution @pointyfar

:+1:

1 Like

Thanks @All

here the complete archetypes/post.md file - if anyone likes it

+++
categories  = ["Post"]
tags        = ["{{ now.Format "2006"}}"]
description = "{{ replace .TranslationBaseName "-" " " | title }}"
title       = "{{ replace .TranslationBaseName "-" " " | title }}"
date        = "{{ .Date }}"
expirydate  = "{{ ((.Date | time ).AddDate 2 0 0).Format "2006-01-02T15:04:05Z07:00"}}"
+++
**Insert Lead paragraph here - goes to summary**
<!--more-->
**Insert the content here**
2 Likes

Really cool, man, thx!