Get Date value of frontmatter .Params object without manual conversion

Hi,

is there a way to get a Time value (so a Time object without manually calling time .Params.somedate) from the .Params object? For .Date this works fine and allows simply filtering and limiting using where and first. However, if I define new params in the frontmatter I only receive them as string and have to convert them manually (which makes where impossible to use nicely).

Thanks a lot of the nice tool otherwise!

Regards,
Erik

Perhaps if you post what you’ve tried, we could get a better idea of what you want?

Hi,

there is little to show actually. But I want that given a content page like:

---
title: foobar
somextraparam: 2018-03-13T18:15:00+02:00
---

to retrieve the {{.Params.somextraparam}} as Date object without doing {{time .Params.somextraparam}} myself. With {{.Date}} this seems to happen automatically, but not with my own field. The reason I need this is because otherwise doing a where selection based on the date is not really possible because I cannot invoke the time command there.

Regards,
Erik

Ah, OK. I think you need the time function.

If you only want to format a date/time string, then you could instead use the
dateFormat function.

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

Hello,

sorry for the delay, I was a bit busy.

Thanks a lot for the very extensive answer @it-gro!

The hint regarding TOML looks promising (is there any way to do the same with yaml?).

I now did the following testing:

Frontmatter:

+++
start = 2018-03-13T18:15:00+02:00
+++

Layout:

{{$start:=.Params.start}}
<li>
<a href="{{.Permalink}}">{{.Title}}</a>:
{{$start.Format "2006-01-02"}}
{{$start.Format "15:04"}}
</li>

This gives me the failure:

ERROR 2018/05/24 15:51:54 in .Render: Failed to execute template "theme/_default/event-summary.html": template: theme/_default/event-summary.html:4:8: executing "theme/_default/event-summary.html" at <$start.format>: can't evaluate field format in type time.Time

So it is a time.Time, but cannot find .Format? :stuck_out_tongue:

If I change the first line to:

{{$start:=time .Params.start}}

it works, even though time claims that it also returns a time.Time result… Am I missing something here?

Regards,
Ablu

You need to use the dateFormat function. While the templates are indeed go templates, I don’t think that they expose native functions of the underlying data types unless implemented in hugo.

EDIT: Just read the bottom part of your post, missed it before responding. Don’t know what the time function does different from the normal time datatype but still if you use the function mentioned above it should work regardless what you pass in, as long as it’s a valid time(string)

You can use (as @totallyinformation stated)

somextraparam = 2018-03-13T20:15:00+02
{{ dateFormat "Monday, Jan 2, 2006" .Params.somextraparam }}
{{ dateFormat "15:04" .Params.somextraparam }}

or

{{ (.Params.somextraparam).Format "Monday, Jan 2, 2006"  }}
{{ (.Params.somextraparam).Format "15:04"  }}

note the ()

=>
Tuesday, Mar 13, 2018
18:15