which can then be parsed by the the template to produce something like:
Company name
July 2012 to September 2014
However, I’m struggling to find a way to do this. I understand that Go templates do not allow you to access arbitrary functions (e.g. time.Parse) and that you must pass in the functions you need as a funcMap. I can’t find any way to do this inside Hugo - ideally it’d be possible to say
{{ parse .Params.startdate "January 2006"}}
but I have no idea how I can make the parse function available without actually modifying Hugo itself.
I take this from memory, but I’m pretty sure time.Parse isn’t available as a template function – neither from Go’s builtins nor Hugo. It could be added, of course, but I’m struggling with understanding the use case.
works fine, because the date field gets automatically cast to a Time.
However,
startdate = "2015-01-17"
and
{{ .Params.startdate.Format "January 2006" }}
does not work, because startdate remains a String rather than being cast to Time.
I have now discovered that you can do the following:
startdate = 2013-07-24T00:00:00Z
and
{{ .Params.startdate.Format "January 2006" }}
because TOML has native support for ISO8601 dates and they are parsed to a Time rather than a String. However, this isn’t ideal, because TOML only supports ‘full Zulu’ dates - you can’t write
startdate = 2013-07-24
In fact, this actually crashes Hugo with the following error:
ERROR: 2015/01/18 Error parsing page meta data for entry.md
ERROR: 2015/01/18 Near line 4, key 'startdate': Near line 5: Expected 'T' in ISO8601 datetime, but found '\n' instead.
ERROR: 2015/01/18 Near line 4, key 'startdate': Near line 5: Expected 'T' in ISO8601 datetime, but found '\n' instead.
CRITICAL: 2015/01/18 Errors rendering pages: Near line 4, key 'startdate': Near line 5: Expected 'T' in ISO8601 datetime, but found '\n' instead.
I can live with writing the ‘full Zulu’ dates for now, but I’d like to find a neater solution. Maybe the answer is that the TOML spec needs changing!
I’m quite new to hugo and am getting the following error:
$ hugo version
Hugo Static Site Generator v0.14-DEV BuildDate: 2015-02-22T08:09:28Z
$ hugo server --theme=lanyon --buildDrafts
ERROR: 2015/02/25 template: theme/post/li.html:1:25: executing "theme/post/li.html" at <.Site.Params.DateFor...>: invalid value; expected string in theme/post/li.html
ERROR: 2015/02/25 Error while rendering page post/post/first.md: template: theme/post/single.html:5:47: executing “theme/post/single.html” at <.Site.Params.DateFor…>: invalid value; expected string
And something pretty similar with the beg theme too. I’ve tried all sorts but I’m not getting very far. Here is what I have:
+++
title = "About Hugo"
date = "2013-06-21T11:27:27-04:00"
menu = "main"
+++
And I’ve tried:
date = "2013-06-21T11:27:27-04:00"
date = 2013-06-21T11:27:27-04:00
date = 2013-06-21
date = "2013-06-21"
With the beg theme I get:
$ hugo server --theme=beg --buildDrafts
ERROR: 2015/02/25 template: theme/_default/li.html:6:39: executing "theme/_default/li.html" at <.Site.Params.DateFor...>: invalid value; expected string in theme/_default/li.html
I used the TOML as an example, since that’s what I work with. If you had that in your config.yaml, it should have picked it up.
Did you have the default config.toml in your directory, alongside the YAML? I don’t know the rules that Hugo uses to pick config files if both are present.
I’m new to Hugo and don’t really understand the DateForm parameter (in the config file).
In the theme, I have: {{ .Date.Format .Site.Params.DateForm }}.
In the config file: DateForm = "02 Jan 2006".
That displays the date as “Sunday, Jul 19, 2015”. Oddly enough, when I remove the DateForm parameter from the theme, it works correctly:
In theme: {{ .Date.Format "02 Jan 2006" }} generates “19 Jul 2015” on the website.
How can I use the DateForm parameter so it outputs “19 Jul 2015”?
Edit: DateForm seems to be working now after I’ve restarted the Hugo webserver a few times now. It seems that changes in the config file aren’t picked up while Hugo is running.
Thanks for the quick reply! I had tried (time $eventDate) and got a parse error, but looking back through my command history I found a typo in my date string. Doh!
Fixing that, using duration "hour", and formatting it back to the expected format works:
Dear all
I know this post is rather old and it has been fixed. Nevertheless I stumbled on a situation where dateFormat does not seem to work.
CONTEXT: I have created a custom taxonomy “time” whose terms take the year and month of the publication date (e.g.: 2021-10).
AIM: I wish to use the year and month in the term to rebuild the date (with an arbitrary day say 02), so the term page shows the terms not as 2021-10 but as 2021 October.
WHAT I DID:
STEP1: Extract year and month from term (which is in the .Page.Title of a {{ range .Data.Terms.Alphabetical.Reverse }})
PageTitle: {{.Page.Title}} → returns 2021-10
{{$year := substr .Page.Title 0 4}} → returns 2021
{{$month := substr .Page.Title 5 2}} → returns 10
STEP2: Construct a string YYYY-MM-DD, taking $year for YYYY, $month for MM, and 02 for DD
{{$xxx := chomp (print $year “-” $month “-02”)}} → returns 2021-10-02
STEP3: Convert the string to a date
{{ dateFormat “2006-01” $xxx }}
The latter fails with the following message: “error calling dateFormat: unable to parse date: 2021–02”
HOWEVER THE FOLLOWING WORKS:
{{$example := “2021-10-02”}}
Original: {{$example}}
Type: {{ printf “%T” $example }}
Result formatted with dateFormat: {{ dateFormat “2006-01” $example}}
Result formatted with time.Format: {{ time.Format “2006-01” $example}}
I have tried playing with print and printf, maybe the value of $xxx should have quotes?
All help greatly appreciated.