Dataview Query Frontmatter Date

Things I have tried

```dataview
LIST
WHERE contains(date-created,{{date:YYYY-MM-DD}})
```

```dataview
LIST
WHERE contains(date-created,<%tp.date.now("YYYY-MM-DD")%>)
```

What I’m trying to do

All of my notes have the create date / time in the frontmatter like this:
date-created: 2023-01-28 08:40

I want my daily note template to list all of the notes created on that date based on the date in the frontmatter instead of file.cday.

This work perfectly but I can’t get it to work with a dynamic date:

LIST
WHERE contains(date-created,“2023-01-28”)

Do you get a better response if you use file.date-created?

WHERE contains(file.date-created,{{date:YYYY-MM-DD}})

Gives me a parsing error.

Is your code within a template since you are using those braces?

Or, what is the context for where you’re calling this query?

WHERE file.date-created = this.file.date-created

Works for me. I have a similar use case in my daily note.

Does this work?

LIST
WHERE contains(string(date-created), string(date(today)) )

My understanding is that the YAML shouldn’t have spaces between dates and times. So, 2023-01-28T11:28 not 2023-01-28 11:28.

And are hyphens not best avoided in key titles?

---
created: 2023-01-28T11:36
---

```dataview
TABLE 
	created as "Created"
WHERE 
	striptime(created) =
	striptime(this.created)
SORT 
	created 
	DESC
```
1 Like

That was the issue! I had a space between the date and time in my templates. Thank you very much, this was driving me nuts!

1 Like

This is trickier than one should aspect mainly due to three reasons:

  1. When using LIST it doesn’t recognise created-date, but needs to use row["created-date"] due to that dash, or minus sign, indicating that you’re actually trying to subtract date from created in the first variant.
  2. Your current format is not actually a date, it’s just a string. To make it a legal date you need to do 2023-01-28T08:40
  3. And even if it was recognised as a date, then a date with a timestamp is not necessarily equal to a date (without that timestamp). That is, 2023-01-28T08:40 is not equal to 2023-01-28.

So how to compensate against all of these reasons?

With no changes in your base data

With no changes in your base data you’ll need something like this monstrosity:

```dataview 
LIST  
WHERE dateformat(date(substring(string(row["created-date"]), 0, 10)), "yyyy-MM-dd") = 
 dateformat(date(substring(this["created-date"], 0, 10)), "yyyy-MM-dd")
```

I wouldn’t go there… This is a beast…

With changes in your base data

I would strongly recommend two changes:

  1. First of all change into proper dates by adding that T into the string, like in 2023-01-28T08:40 (or simpler, just remove the timestamp, and just leave 2023-01-28)
  2. Change from created-date into created_date, which doesn’t need all that special care through using row["created-date"] to access it

If you do both of these, you’ll end up almost with what you started with:

```dataview
LIST
WHERE created_date = this.created_date
```

Or if you don’t want to loose the timestamp

This depends on whether you’ve got the timestamp included in all files, or just this file. If in all files, you’ll need to also change the part related to created_date accordingly to the this.created_date.

```dataview
LIST
FROM created_date = dateformat(this.created_date, "YYYY-MM-DD")
```

So there you have it, changes need to be made, if you want to avoid the monstrosity in the beginning. Which changes kind of depends on how many notes we’re talking about, and which format you’ve already got for the created-date in those notes.

3 Likes

Wow, I wrote a monstrosity of a post with multiple examples, and you say the same in just a few sentences. Sometimes I feel like I’m overcomplicating stuff for no reason.

Kudos to you.

Regarding hyphens, they do work in TABLE queries, just not LIST queries it seems.

3 Likes

:exploding_head: :pray:

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