Edit: as explained in my subsequent post, there’s nothing wrong with passing variables to time.Format, provided that variable has a string value.
I’m struggling to get time.Format to accept data read from a json file.
The original data looks like
{ "startDate": "2023-01-28T15:00" }
Which I’ve put in a variable $json_data.startDate
What I’m essentially trying to do is
{{ time.Format "Monday, Jan 2, 2006" "2023-01-28" }}
Except passing “2023-01-28” as a variable. The snag I’ve hit is
{{ time.Format "Monday, Jan 2, 2006" (substr $json_map.startDate 0 10) }}
causes running hugo to barf:
$ hugo
Start building sites …
hugo v0.108.0+extended linux/amd64 BuildDate=unknown
ERROR 2023/01/12 08:33:19 render of "page" failed: "/home/roblaing/webapps/joeblog/themes/joetheme/layouts/_default/single.html:8:7": execute of template failed: template: _default/single.html:8:7: executing "main" at <time.Format>: error calling Format: unable to parse date:
Error: Error building site: failed to render pages: render of "page" failed: "/home/roblaing/webapps/joeblog/themes/joetheme/layouts/_default/single.html:8:7": execute of template failed: template: _default/single.html:8:7: executing "main" at <time.Format>: error calling Format: unable to parse date:
Total in 40 ms
There’s something clearly wrong with the type of the substring. But
{{ eq (substr $json_map.startDate 0 10) "2023-01-28" }}
is true, so I’m at a loss at how to transform the input.
I found a similar question from a while back at Date in Template from Data (json) but couldn’t figure out the solution from the replies.