How do I use where with now function

I’ve tried several different constructs as shown below, but I can’t get this statement to work. No results are shown. If I change “lt” to “!=” it works, but gives the wrong results. What am I doing wrong?

Front Matter

date = 2020-06-01T08:00:00+08:00
event_date = 2020-06-11T08:00:00+08:00
title = "My Event"

.Params.event_date is set in archtypes: {{(.Date.AddDate 0 0 10).Format "2006-01-02"}}.

{{$pages := where .Pages .Params.event_date "lt" now}}

{{$pages := where .Pages (time .Params.event_date) "lt" now}}

{{$pages := where .Pages (time .Params.event_date) "lt" (now.Format "2006-01-02T15:04:05-0700")}}

{{$pages := where .Pages (time .Params.event_date) "lt" (now.Format "2006-01-02")}}

Please show an example of front matter, including the delineators.

I’ve updated my post with my front matter. Thank you.

  {{ range .Pages.ByParam "event_date" }}
    {{ if .Params.event_date.Before now }}
      <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
      <p>{{ .Summary }}</p>
    {{ end }}
  {{ end }}

You don’t need to cast event_date to time. Hugo sees an unquoted TOML timestamp as type time.Time. If you were to use JSON, YAML, or a quoted value in TOML, you would need to cast to time before doing the comparison.

EDIT: I don’t think there’s any way of doing this with a where clause without converting event_date and now to integers.

Thank you. Appreciate the reply and code.

I’ve previously got this to work with an if statement, but was hoping to understand why this doesn’t work with a where clause as in my OP. I’ll look into converting the dates to integers and see if that works.

{{ range where .Pages "Params.event_date.Unix" "lt" now.Unix }}
  <h2><a href="{{ .RelPermalink }}">{{ .Title }}</a></h2>
  <p>{{ .Summary }}</p>
{{ end }}
1 Like

Thank you! That works.

i was just scrolling through this article and about to give unix a go. Thanks again for your time.

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.