Dynamic expiryDate in archetype

I’m trying to make the expiryDate dynamic by adding 30 days to the date param.

I have this in my default archetype.

---
title: "{{ replace .Name "-" " " | title }}"
date: "2024-03-23"
expiryDate: "{{ dateFormat "2006-01-02" ( .Date.AddDate 0 0 30 ) }}"
draft: true
---

Thank you.

You are currently processing the archetype so you cannot access the properties you set before (guess implementation and performance considerations.)

but you may store stuff in variables

---
{{- $mydate := "2024-03-25" }}
title: '{{ replace .File.ContentBaseName "-" " " | title }}'
date: "{{- $mydate -}}"
expiryDate: '{{- (time.AsTime $mydate).AddDate 0 0 30 | time.Format "2006-01-02" -}}'
---

which produces with hugo new content test.md

---
title: 'Test'
date: "2024-03-25"
expiryDate: '2024-04-24'
---

keep an eye on: [problems with date calculation:] mentioned in (AddDate | Hugo)
Calculate first day of current month and last day of current month - #4 by privatemaker

1 Like

So i have to store in a variable.

Thank you so much!

If you want to set the expiry date based on the creation date:

+++
title = '{{ replace .File.ContentBaseName "-" " " | strings.FirstUpper }}'
date = {{ .Date }}
expiryDate = {{ (now.AddDate 0 0 30).Format "2006-01-02T15:04:05-07:00" }}
+++

Note that .Date with an archetype template returns a string representation (RFC3339) of time.Now.

https://gohugo.io/content-management/archetypes/#functions-and-context

1 Like

Thank you.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.