# Parsing custom date format (French, Spanish, ...)

**URL:** <https://discourse.gohugo.io/t/parsing-custom-date-format-french-spanish/39463>\
**Category:** support\
**Created:** [July 12, 2022, 5:11pm UTC](https://discourse.gohugo.io/t/parsing-custom-date-format-french-spanish/39463 "2022-07-12T17:11:42Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![BenSouchet](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bensouchet/32/17297_2.png) [@BenSouchet](https://discourse.gohugo.io/u/BenSouchet)\
**Post date:** [July 12, 2022, 5:11pm UTC](https://discourse.gohugo.io/t/parsing-custom-date-format-french-spanish/39463/1 "2022-07-12T17:11:42Z")

</div>

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:

```auto
{{< 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](https://pkg.go.dev/time?utm_source=godoc#ParseInLocation)), 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`:

```auto
params:
  dateFormat: "DD/MM/YYYY"

```

Then in the shortcode `parseDate.html`:

```auto
{{ $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](https://go.dev/play/p/MUmEkG2826O))

## 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!

---

<div class="post-metadata">

**Author:** ![jmooring](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/jmooring/32/4214_2.png) [@jmooring](https://discourse.gohugo.io/u/jmooring)\
**Post date:** [July 12, 2022, 6:43pm UTC](https://discourse.gohugo.io/t/parsing-custom-date-format-french-spanish/39463/2 "2022-07-12T18:43:21Z")

</div>

First, you cannot parse _either_ of these formats with the `time.AsTime` method:

```nohighlight
{{ time.AsTime "07/12/2022" }} --> error calling AsTime: unable to parse date
{{ time.AsTime "12/07/2022" }} --> error calling AsTime: unable to parse date

```

Second, although this is not what you asked for, the simplest approach is to use the (less ambiguous) ISO 8601 format:

```nohighlight
{{ time.AsTime "2022-07-12" }}

```

Finally, if you want to use the `dd/mm/yyyy` format in your shortcode parameters, you could do something like:

layouts/shortcodes/cvSingleElement.html

```nohighlight
{{ $startDate := partial "cast-to-time.html" (.Get "startdate") }}
{{ $endDate := partial "cast-to-time.html" (.Get "enddate") }}

```

layouts/partials/cast-to-time.html

```auto
{{ $dateParts := split . "/" }}
{{ if lt (len $dateParts) 3 }}
  {{ errorf "Cannot cast %s to time.Time value" . }}
{{ end }}
{{ return (time.AsTime (printf "%s-%s-%s" (index $dateParts 2) (index $dateParts 1) (index $dateParts 0))) }}

```

---

<div class="post-metadata">

**Author:** ![BenSouchet](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bensouchet/32/17297_2.png) [@BenSouchet](https://discourse.gohugo.io/u/BenSouchet)\
**Post date:** [July 12, 2022, 7:10pm UTC](https://discourse.gohugo.io/t/parsing-custom-date-format-french-spanish/39463/3 "2022-07-12T19:10:24Z")

</div>

Thanks for the workaround.  
**But** I would be better to have a _true_ way to parse time like in Go we can use `time.ParseInLocation()` as you can see here : [Go Playground - The Go Programming Language](https://go.dev/play/p/gsSIgLgAYLL)  
(and the `timeZone` config variable already exists in Hugo)

Imagine this shortcode if for a Hugo theme shared will people from different countries, one use the 2006-01-02 format, another one use the 02/01/2006, another one use 02.01.2006 (like in Finland).

So without a proper “fix”, like adding a Hugo method `time.Parse` like the Parse method in Go, the workaround to handle all these common cases will be more like a big “hack” than a solution.

For now I will use the workaround you shared but still interested to get the point of view of the others members and devs.

---

<div class="post-metadata">

**Author:** ![chrillek](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/chrillek/32/19797_2.png) [@chrillek](https://discourse.gohugo.io/u/chrillek)\
**Post date:** [July 13, 2022, 12:46pm UTC](https://discourse.gohugo.io/t/parsing-custom-date-format-french-spanish/39463/4 "2022-07-13T12:46:22Z")

</div>

Although I feel your pain, I guess there’s no point insisting on using national formats for _internal_ purposes (I’m not talking about representation here!). If you need to convert those dates to whatever, you’re better off with ISO format.

> [@BenSouchet](#):
>
> Imagine this shortcode if for a Hugo theme shared will people from different countries, one use the 2006-01-02 format, another one use the 02/01/2006, another one use 02.01.2006 (like in Finland).

I’m wondering about a US person living in Finnland… or maybe a German one living in the US. Are they going to use the US, the Finnish or the German format? That’s the charme of the ISO dates: You do _not_ have to think about all these border cases since the format is always the same.

---

<div class="post-metadata">

**Author:** ![BenSouchet](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bensouchet/32/17297_2.png) [@BenSouchet](https://discourse.gohugo.io/u/BenSouchet)\
**Post date:** [July 13, 2022, 1:09pm UTC](https://discourse.gohugo.io/t/parsing-custom-date-format-french-spanish/39463/5 "2022-07-13T13:09:46Z")

</div>

I don’t understand your point, the objective is to facilitate the access to Hugo to the users and you want them to understand by themselves that due to a missing function they are forced to use a date formatted to the ISO standard.  
For example, does your cousin or your nephew know the ISO standard of date formatting?

In the GO language there is `time.ParseInLocation()` and in Hugo users can set in there config file the `timeZone` variable.

So why not adding the ability to Parse date properly in Hugo?  
Why forcing everyone using the ISO format to be able to cast this into a time ?

All the cases can be resolve by just adding a `time.Parse` method in Hugo that simply call the GO `time.ParseInLocation()` to return a `time.time object`.

For Hugo to be used by more people you have to stop thinking in terms of dev and think a bit in terms of user.

Sorry to be hard on you people but the answer “use the ISO format” isn’t a solution at all.

@bep , do you have an opinion on the matter ?

**PS:** This isn’t for internal usage, end users can use shortcodes in there Markdown pages and file `startdate` parameter to build there CV page.

---

<div class="post-metadata">

**Author:** ![bep](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bep/32/3332_2.png) [@bep](https://discourse.gohugo.io/u/bep)\
**Post date:** [July 13, 2022, 2:10pm UTC](https://discourse.gohugo.io/t/parsing-custom-date-format-french-spanish/39463/6 "2022-07-13T14:10:18Z")

</div>

> [@BenSouchet](#):
>
> @bep , do you have an opinion on the matter ?

My opinion on the matter is that you have too simplistic world view on this matter. You may find this issue worth reading:

> <https://github.com/spf13/cast/pull/80>

That said,

- You can set `timeZone` per language in your site config.
- That `timeZone` will be used for most date parsing in Hugo (I say most, because there are some that is currently out of control, e.g. JSON/YAML date types).
- That value will also be used in the `time.Format` template func which I recommend you use for all date formatting.

---

<div class="post-metadata">

**Author:** ![BenSouchet](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bensouchet/32/17297_2.png) [@BenSouchet](https://discourse.gohugo.io/u/BenSouchet)\
**Post date:** [July 13, 2022, 2:55pm UTC](https://discourse.gohugo.io/t/parsing-custom-date-format-french-spanish/39463/7 "2022-07-13T14:55:02Z")

</div>

@bep Yes I know `timeZone` is used in HUGO but there is no possibility to parse with a “custom” format.

So ability to parse date with a timeFormat that isn’t in the hardcoded list like for _French, Thai, Finnish, Spanish_ isn’t relevant ?

When you have a website and you need to enter a date in a calendar you enter **MM-DD-YYYY** or **DD/MM/YYYY** so why in hugo user passing date to a shortcode is forced to use **YYYY-MM-DD**?

Last point, since in the hardcoded timeFormat array there is **“02 Jan 2006”** , why not having `02/01/2006` and `02.01.2006`.

**Or** having a Hugo method:

```auto
{{ time.ParseWithFormat FORMAT INPUT [TIMEZONE] }}

{{ time.ParseWithFormat "02/01/2006" "15/03/2021" }} → "2021-03-15T00:00:00Z"

```

So user can specified in config Params the date format they used in there HUGO site and the theme can do:

```auto
{{ $startdate := .Get "startdate" }}
{{ if isset .Site.Params "dateformat" }}
{{ $startdate = time.ParseWithFormat .Site.Params.dateformat $startdate }} 
{{ else }}
{{ $startdate = time.AsTime $startdate }}
{{ end }}

```

And of course if user didn’t specified the dateformat and the input cannot be cast an error will be raise.

---

<div class="post-metadata">

**Author:** ![BenSouchet](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/bensouchet/32/17297_2.png) [@BenSouchet](https://discourse.gohugo.io/u/BenSouchet)\
**Post date:** [July 17, 2022, 10:51am UTC](https://discourse.gohugo.io/t/parsing-custom-date-format-french-spanish/39463/8 "2022-07-17T10:51:22Z")

</div>

For the people who would come across this post, I opened a Feature issue on the Github project:

> <https://github.com/gohugoio/hugo/issues/10091>
>
> \## The issue
> 
> Currently there is no way to parse date string(s) with custom la…yout, like the French one \`DD/MM/YYYY\`, the Spanish one \`DD-MM-YY\`, the Finnish one \`DD.MM.YYYY\`.
> 
> \## Proposal
> 
> A hugo time method to parse date string with a provided layout like this:
> \`\`\`go
> {{ time.ParseWithFormat FORMAT INPUT \[TIMEZONE\] }}
> 
> {{ time.ParseWithFormat "02/01/2006" "15/03/2021" }} → "2021-03-15T00:00:00Z"
> \`\`\`
> 
> \## How to
> 
> Internally this would use the Go time function \`ParseInLocation\` like this:
> \`\`\`go
> time.ParseInLocation("02/01/2006", "15/03/2021", loc)
> \`\`\`
> 
> Go Playground example: https://go.dev/play/p/gsSIgLgAYLL
