Specify date format in archetype?

Given that Hugo recently processes archetype as template files (since version 0.24), I’m trying to update my archetypes. One change I want to make is pre-fill the dates in the right format, like so:

date 			= "{{ dateFormat .Date "2-1-2006" }}"

Which Hugo doesn’t like:

executing “hugo” at <dateFormat .Date "2-…>: error calling dateFormat: unable to parse date: 2-1-2006

Using .Format neither works:

date 			= "{{ .Date.Format "2-1-2006" }}"

executing “hugo” at <.Date.Format>: can’t evaluate field Format in type string

Am I missing something, or is this simply not possible?


PS:

  • I have no trouble executing the replace function in my archetype file.
  • Escaping the inner double-quotes with \ doesn’t help.

Hugo Static Site Generator v0.30.2 windows/amd64 BuildDate: 2017-10-20T12:44:44+02:00
GOOS="windows"
GOARCH="amd64"
GOVERSION="go1.9.1"

Okay, this got really interesting!

I made a test archetype to play with formatting, see if I could replicate the error. And sure enough, it shows the same for me.

+++
title = "blah"
date = "{{ dateFormat .Date "2-1-2006" }}"
+++
template: date:3:11: executing "date" at <dateFormat .Date "2-...>: error calling dateFormat: unable to parse date: 2-1-2006

Then I was getting a refresher (meaning I never remember this stuff) on Go’s layout string in the docs, where I remembered there being a specific example: "2006-01-02"

+++
title = "blah"
date = "{{ dateFormat .Date "2006-01-02" }}"
+++
+++
title = "blah"
date = "2017-10-29T00:06:02+00:00"
+++

Then I decided to move them around one at a time, to see if we could pinpoint the reason even 02-01-2006 doesn’t work. My next test was for 2006-02-01

+++
title = "blah"
date = "{{ dateFormat .Date "2006-02-01" }}"
+++
+++
title = "blah"
date = "1027-20-19T00:09:00+00:00"
+++

:astonished:

:thinking:

:face_with_raised_eyebrow:

Okay, let’s see what we get! I tried all combos (there were only six for the portions we are messing with), and only the format with the year at the beginning is parsing.

+++
title = "blah"
date = "{{ .Date }}"
+++

{{ dateFormat .Date "2006-01-02" }}
{{ dateFormat .Date "2006-02-01" }}
+++
title = "blah"
date = "2017-10-29T01:09:09-07:00"
+++

2017-10-29T01:09:09+00:00
1027-20-19T02:09:09+00:00

These don’t work:

  • 02-2006-01
  • 02-01-2006
  • 01-02-2006
  • 01-2006-02

So, I hope we figure out @Jura’s thing, but I am also very curious as to why using 2006-02-01 produces 1027-20-19, where every other number is switched as a pair. :slight_smile:

1 Like

Thanks for confirming Maiki. I’ve opened a GitHub issue here for this situation.

.Date is in this context a preformatted string (you may ask me if that was a clever choice, but I’m not going bak on that one.

Do this:

+++
title = "blah"
date = "{{ now.Format "2006-01-02" }}"
+++
5 Likes

Obviously Bjørn is correct, and to demonstrate:

archetypes/date.md

+++
title = "{{ .Date }}"
date = "{{ .Date }}"
+++

{{ now.Format "2006-01-02" }}
{{ now.Format "2006-02-01" }}
{{ now.Format "02-2006-01" }}
{{ now.Format "02-01-2006" }}
{{ now.Format "01-20-2006" }}
{{ now.Format "01-2006-02" }}

Produces content/date/nowformat.md

+++
title = "2017-11-06T01:02:52-08:00"
date = "2017-11-06T01:02:52-08:00"
+++

2017-11-06
2017-06-11
06-2017-11
06-11-2017
11-60-2017
11-2017-06

Super handy! :slight_smile:

1 Like

What about something like this:

last_update: {{ dateFormat "2006-01-02" .Date }}

It’s working for me right now.

Also, it looks like the error was caused by the params being swapped around.
First, you need to pass the date layout , then you pass the date.

That is a little bit ineffective. Better:

last_update: {{ now.Format "2006-01-02" }}

I think this needs to be added to the documentation. I just spent an hour trying to figure this out before I found this thread.

There is an example here

2 Likes

Missed that, thank you!