Changing date format in front matter

Is there a simple way to convert dates from a format like 14 Jan 2006 to 2006-01-14?

You can convert a string to a date via time.AsTime, but I don’t understand your use case, are you using custom date parameters other than Date, PublishDate or other built-in date’s vars in front matter?

{{ ("14 Jan 2006" | time.AsTime).Format "2006-01-02" }}

I mean in my markdown files, not in templates.

You can use the archetype for new content.

---
date: {{ (.Date | time.AsTime).Format "2006-01-02" }}
---
1 Like

I personally don’t like starting files with `hugo new content blabla", but more importantly frontmatters are static and if you’re putting dates on your site, then create dates don’t really matter for people, unlike publishing date or last modification dates, which don’t / can’t go in the frontmatter. Because it’s not adding any information, I think it’s more flexible to just use the date formatting functions wherever you need in your templates/partials.

1 Like

What about existing content?

I think you’ve to write a script to replace the date with your new format for previous contents, but the date format in contents doesn’t matter to me, it makes more sense that to format it in templates.

Did everything manually (tedious but worked).

1 Like

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