Hi,
After looking at the docs and discussions I didn’t find an answer to the question:
Is it possible in a Hugo template or Shortcode file to parse custom date formats ?
Context:
Imagine a CV page theme created via Hugo (“CV.md”), this CV page is made of shortcodes for example one like this:
{{< cvSingleElement company="Google" title="Software R&D Engineer" location="Paris area, France" startdate="15/03/2021" enddate="10/09/2021" >}}
Job description here.....
{{</ cvSingleElement >}}
As you can see there is two shortcode variables startdate
and enddate
, these dates aren’t using the US format but the French one (see this page : https://docs.oracle.com/cd/E19455-01/806-0169/overview-7/index.html
)
In the shortcode file I want to parse these date and do some operation to display this:
Mar 2021 - Sep 2021 (6 mos)
The Issue:
Currently it seems I cannot convert / parse these dates strings into time object since they aren’t “US style” formatted.
There is Go functions that do this job like with time.ParseInLocation()
(see here), but currently not available in Hugo.
A not working idea:
I tried to create a workaround by creating a parseDate shortcode in hugo using a config string named dateFormat
:
params:
dateFormat: "DD/MM/YYYY"
Then in the shortcode parseDate.html
:
{{ $format := `` }}
{{ if isset .Site.Params "dateformat" }}
{{ $format = .Site.Params.dateFormat }}
{{ else if .Get "dateformat" }}
{{ $format = .Get "dateformat" }}
{{ else }}
{{ errorf "dateformat is required, set it on your params config file or pass itas named parameter." "" }}
{{ end }}
{{ $format = replaceRE "DD" "(?P<day>\\d{2})" $format }}
{{ $format = replaceRE "MM" "(?P<month>\\d{2})" $format }}
{{ $format = replaceRE "YYYY" "(?P<year>\\d{4})" $format }}
{{ $format = delimit (slice "^" $format "$") "" }}
{{ $format }}
{{ $date := .Get 0 }}
{{ $findResult := findRE `^(?P<day>\d{2})/(?P<month>\d{2})/(?P<year>\d{4})$` $date }}
But I got two big issues:
1. As you can see in the last line I didn’t used $format
since somehow the variable was a string:
"^(?P<day>\d{2})-(?P<month>\d{2})-(?P<year>\d{4})$"
and not a regex string like:
`^(?P<day>\d{2})-(?P<month>\d{2})-(?P<year>\d{4})$`
Notice backticks instead of " "
2. And the most important issue, findRE ignore groups since internal the method used is FindAllString
and not FindStringSubmatch
(check this for live demo)
Question
Is it possible or planned to be able to parse international date in Hugo ?
it’s frustrating since if you have a valid time you can format it like you wish, but it’s not possible to parse it.
Thanks for your time,
Long life to Hugo and thanks for the devs and the people of this forum!