Can't figure out using `where` with dates

This has already been discussed, but I still can’t for the life of me decipher what I’m doing wrong. Previous thread: Trying to filter data files based upon a date field

I want to filter out events that have already happened on my site. I’m trying this:

{{ $shows := where .Site.RegularPages "Section" "shows" }}
{{ range where $shows "Params.showDate" "gt" now }}

and in the frontmatter of a exampleshow.md:

---
showDate: 2023-12-21T00:29:49.000Z
---

I have tried a bunch of different formats for showDate but nothing seems to be working. The only thing I could get to work was by converting the dates to Unix timestamps, but then time.Format doesn’t know how to decode the time (for display in the page), so that’s not very helpful.

Indirectly related, I am setting this up with Decap CMS, so I can use their dateTime widget to set the showDate, but I don’t know what format I need to use. Widgets | Decap CMS | Open-Source Content Management System

WIP repo is at: GitHub - schaefsteven/rocky-hutchins at testing

This is covered in the documentation:
https://gohugo.io/templates/introduction/#example-show-future-events

There are two methods shown, depending on the format of your front matter.

Thanks! Didn’t find this part of the docs while I was searching. Maybe there should be a mention of this on the page for where?

I ended up switching my event pages to use toml frontmatter so I could use it with where and have a proper range with an else for if the range is empty.

Good choice. I tend to use TOML everywhere with unquoted dates so that I don’t have to think about it. Note that the four predefined front matter dates are always unmarshaled to time.Time values, regardless of front matter format

1 Like

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