# Trying to filter data files based upon a date field

**URL:** <https://discourse.gohugo.io/t/trying-to-filter-data-files-based-upon-a-date-field/2114>\
**Category:** support\
**Created:** [November 20, 2015, 6:55am UTC](https://discourse.gohugo.io/t/trying-to-filter-data-files-based-upon-a-date-field/2114 "2015-11-20T06:55:42Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![mattstratton](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mattstratton/32/24584_2.png) [@mattstratton](https://discourse.gohugo.io/u/mattstratton)\
**Post date:** [November 20, 2015, 6:55am UTC](https://discourse.gohugo.io/t/trying-to-filter-data-files-based-upon-a-date-field/2114/1 "2015-11-20T06:55:43Z")

</div>

I am building a site that will have multiple events, and we need to be able to determine which events are current and which are in the past.

Since each event will have multiple pages associated with it, the desire is to use a data file for each event, which will then drive a bunch of templates.

I have tried putting in a `startdate` and `enddate` field in the TOML file for each event (I started by trying to do it with YAML, but then suspected that maybe hugo was having problems parsing what YAML thought was a date field, but this seems to be a red herring).

Here’s an example data file:

```auto
name = "kiel2016"
year = "2016"
city = "Kiel"
startdate = "2016-05-12T23:29:49-06:00"
enddate = "2016-05-13T23:29:49-06:00"

```

Then this is where I am attempting to simply display the events that are in the future:

```auto
      {{ range $e := $.Site.Data.events }}
        {{ if ge $e.startdate .Now }}
          {{ .city }} {{ .startdate }}<br />
        {{ end }}
      {{ end }}
current date {{ .Now }}

```

The problem is, all events show up…

```auto
Chicago 2015-08-25T23:29:49-06:00
Kiel 2016-05-12T23:29:49-06:00
current date 2015-11-20 00:53:43.757705937 -0600 CST

```

Thoughts?

---

<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:** [November 20, 2015, 7:35am UTC](https://discourse.gohugo.io/t/trying-to-filter-data-files-based-upon-a-date-field/2114/2 "2015-11-20T07:35:24Z")

</div>

> [@mattstratton](#):
>
> startdate = “2016-05-12T23:29:49-06:00”  
> enddate = “2016-05-13T23:29:49-06:00”

Read the TOM (or YAML) spec. That aren’t dates. That are strings.

---

<div class="post-metadata">

**Author:** ![mattstratton](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mattstratton/32/24584_2.png) [@mattstratton](https://discourse.gohugo.io/u/mattstratton)\
**Post date:** [November 20, 2015, 7:59am UTC](https://discourse.gohugo.io/t/trying-to-filter-data-files-based-upon-a-date-field/2114/3 "2015-11-20T07:59:53Z")

</div>

Huh. They’re the same format as in frontmatter in TOML in a post, so I figured they would work that way.

I just tried changing it to this:

```auto
startdate = 2016-05-12T23:29:49Z
enddate = 2016-05-13T23:29:49Z

```

Which matches the spec listed at [https://github.com/toml-lang/toml#user-content-datetime](https://github.com/toml-lang/toml#user-content-datetime) and I get the same issue…

Now the output looks like this:

```auto
Chicago 2015-08-25 23:29:49 +0000 UTC
Kiel 2016-05-12 23:29:49 +0000 UTC
current date 2015-11-20 01:58:18.387917495 -0600 CST

```

FYI, here’s a link to the branch with the offending code. [https://github.com/devopsdays/devopsdays-web/tree/filter-upcoming-events](https://github.com/devopsdays/devopsdays-web/tree/filter-upcoming-events)

The actual file where the search/filtering is happening is [https://github.com/devopsdays/devopsdays-web/blob/filter-upcoming-events/themes%2Fdevopsdays%2Flayouts%2Findex.html](https://github.com/devopsdays/devopsdays-web/blob/filter-upcoming-events/themes%2Fdevopsdays%2Flayouts%2Findex.html)

---

<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:** [November 20, 2015, 8:12am UTC](https://discourse.gohugo.io/t/trying-to-filter-data-files-based-upon-a-date-field/2114/4 "2015-11-20T08:12:29Z")

</div>

> [@mattstratton](#):
>
> {{ if ge $e.startdate .Now }}

Try

```auto
{{ if ge $e.startdate.Unix .Now.Unix }}

```

`ge` and similar should support dates directly, I will try to remember to create an issue for that later …

> [@mattstratton](#):
>
> Huh. They’re the same format as in frontmatter in TOML in a post, so I figured they would work that way.

For front matter Hugo parses the date strings into dates. Makes us more flexible with date formats (for people coming from Jekyl etc.). We can not do the same “guessing” for data files.

---

<div class="post-metadata">

**Author:** ![mattstratton](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mattstratton/32/24584_2.png) [@mattstratton](https://discourse.gohugo.io/u/mattstratton)\
**Post date:** [November 20, 2015, 8:20am UTC](https://discourse.gohugo.io/t/trying-to-filter-data-files-based-upon-a-date-field/2114/5 "2015-11-20T08:20:31Z")

</div>

Want to see something REALLY weird now?

Updated the code thusly:

```auto
      {{ range $e := $.Site.Data.events }}
        {{ if ge .startdate.Unix .Now.Unix }}
          {{ .city }} {{ .startdate.Unix }}<br />
        {{ end }}
      {{ end }}
      current date {{ .Now.Unix }}

```

And the output looks like this:

```auto
Chicago 1440545389
Kiel 1463095789
current date 1448007493

```

So it’s clearly converting it into the Unix timeformat, but still not actually doing the logic. I feel like now I’m missing something simple. It would be even more clean if I could embed it all into a where clause in the initial select, but at this time of night my brain can’t parse it…

---

<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:** [November 20, 2015, 10:28am UTC](https://discourse.gohugo.io/t/trying-to-filter-data-files-based-upon-a-date-field/2114/6 "2015-11-20T10:28:40Z")

</div>

That doesn’t make sense…

Anyhow, see

> <https://github.com/gohugoio/hugo/issues/1593>

---

<div class="post-metadata">

**Author:** ![mattstratton](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mattstratton/32/24584_2.png) [@mattstratton](https://discourse.gohugo.io/u/mattstratton)\
**Post date:** [November 20, 2015, 3:17pm UTC](https://discourse.gohugo.io/t/trying-to-filter-data-files-based-upon-a-date-field/2114/7 "2015-11-20T15:17:44Z")

</div>

OK, so if I want to move it to a `where`, it should look something like this? I know it’s not right, because it blows up, but am I getting close?

`{{ range where .Site.Data.events "startdate" "ge" "Now.Unix" }}`

---

<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:** [November 20, 2015, 4:39pm UTC](https://discourse.gohugo.io/t/trying-to-filter-data-files-based-upon-a-date-field/2114/8 "2015-11-20T16:39:45Z")

</div>

> [@mattstratton](#):
>
> {{ range where .Site.Data.events “startdate” “ge” “Now.Unix” }}

This should work:

```auto
{{ range where .Site.Data.events "startdate.Unix" "ge" .Now.Unix }}

```

In Hugo 0.15 this should also work:

```auto
{{ range where .Site.Data.events "startdate" "ge" .Now }}

```

---

<div class="post-metadata">

**Author:** ![mattstratton](https://yyz2.discourse-cdn.com/flex036/user_avatar/discourse.gohugo.io/mattstratton/32/24584_2.png) [@mattstratton](https://discourse.gohugo.io/u/mattstratton)\
**Post date:** [November 20, 2015, 7:10pm UTC](https://discourse.gohugo.io/t/trying-to-filter-data-files-based-upon-a-date-field/2114/9 "2015-11-20T19:10:18Z")

</div>

> [@bep](#):
>
> {{ range where .Site.Data.events “startdate.Unix” “ge” .Now.Unix }}

I tried that. Hugo throws this error:

`ERROR: 2015/11/20 Error while rendering homepage: template: theme/index.html:24:15: executing "theme/index.html" at <where .Site.Data.eve...>: error calling where: can't iterate over map[string]interface {}`
