Get Date value of frontmatter .Params object without manual conversion

Are there any Date Objects?
You probably mean the date variable: Page variables | Hugo
Their data type is time.Time
time package - time - Go Packages

Given the front matter:

Time1          = 0001-01-01T07:32:00Z
Time2          = 0001-01-01T08:00:00Z
Time3          = "07:32:00"
somextraparam  = 2018-03-13T18:15:00+02:00
somextraparam2 = 2018-03-13T20:15:00+02:00

Check here: Date in Template from Data (json) - #6 by bep
=> No quotes for time datatype.

Toml says:

If you include only the time portion of an RFC 3339 formatted
date-time, it will represent that time of day without any relation
to a specific day or any offset or timezone.

This does not work in front matter:

Time3          = 07:32:00

=> failed to parse page metadata
So we can’t just provide time (hour,min,sec) information - it has to be time.Time.

But we do have options in the front matter

  1. using zeros as date part (year, month, day) 0001-01-01
  2. using a string “07:32:00”
{{ printf "`%s` is `%T`" .Page.Lastmod .Page.Lastmod }}
{{ printf "`%s` is `%T`" .Params.Time1 .Params.Time1 }}
{{ printf "`%s` is `%T`" .Params.Time3 .Params.Time3 }}
{{ printf "`%s` is `%T`" .Params.somextraparam .Params.somextraparam }}

=>

`2018-05-09 23:40:12 +0200 CEST` is `time.Time`
`0001-01-01 07:32:00 +0000 UTC` is `time.Time`
`07:32:00` is `string`
`2018-03-13 18:15:00 +0200 +0200` is `time.Time`

So data type is time.Time - but “07:32:00” is just a string.

=> "manually use the time function (to convert data type):

{{ $myTime := time (printf "0001-01-01T%s" .Params.Time3) }}
{{ printf "`%s` is `%T`" $myTime $myTime }}

=>

`0001-01-01 07:32:00 +0000 UTC` is `time.Time`

You can do:

{{ (.Params.somextraparam2).Sub .Params.somextraparam }}

=>
2h0m0s

This is then a new datatype:

{{ printf "`%s` is `%T`" ((.Params.somextraparam2).Sub .Params.somextraparam) ((.Params.somextraparam2).Sub .Params.somextraparam) }}
`2h0m0s` is `time.Duration`

So bottom line:

to retrieve the {{.Params.somextraparam}} as Date object

  • you do get a variable of type time.Time
  • as all other such variables are (like .Page.Lastmod)
  • no quotes!
  • there should be no difference …
1 Like