Working with dates derived from file names to return files created on the same day and month

What I’m trying to do

I use the Daily note plugin with a template. Each daily note is automatically created with the date (YYY-MM-DD format) as a file name. I want to write a DataViewJS (or DataView) query that works with the file name string as a date and compares it to the creation date value of all the other files in the vault. How can I write code that works with these two different value types to return a table showing the name and creation date of all the files in the vault that were created on the same day and month found in the title of the parent note?

Things I have tried

I currently use the query below but it draws from the parent files creation date; not the file name. Without referencing the date reflected in the file name, I can’t create daily notes for future dates without botching the query results. This also wreaks havoc when I miss creating a daily note and go back and create one on a later date.

TABLE WITHOUT ID file.link AS "Note", file.cday AS "Date"
WHERE file.ctime.month = this.file.ctime.month AND file.ctime.day = this.file.ctime.day AND file.name !=(this.file.name)
SORT file.ctime.year DESC

I’ve tried modifying code from other posted topics but other than finding a DataViewJS query that works with dates in the file name DataviewJS get files matching same date as note’s title :link: I’ve had no luck. In the thread @holroy mentioned:

Within dataviewjs you can’t use this.file.name , you need to do something like dv.current().file.name .

However the original poster didn’t choose to use that in the final solution so I’m not sure how to work with the property using JavaScript.

1 Like

If I understand, you have a note “2024-01-22.md” and you want to show all the notes created in the same day and month.

To get all the notes created in the exact same year-month-day you can copy this in your note:

```dataview
TABLE file.cday
WHERE file.cday = this.file.day
```

And for all the notes created in the same month and day:

```dataview
TABLE file.cday
WHERE file.cday.day = this.file.day.day
AND file.cday.month = this.file.day.month
```

With file.day you can get the date in the title of the note.

2 Likes

In the Dataview documentation we find this related to file.day:

file.day (Date) – Only available if the file has a date inside its file name (of form yyyy-mm-dd or yyyymmdd), or has a Date field/inline field.

This means that in your queries since you’ve got a matching file name, you’ve got access to the file.day variable which then has translated your text string into a proper date. (If that hadn’t been a case you’d need to do date(yourTextualDateString) or similar to change it into a date)

For the current file in an ordinary query you can use this.file.day to access that date, and within dataviewjs you could use dv.current().file.day to access that date.

@sanyaissues has already shown one way of doing this in an ordinary query. A variation within those queries could also be to use dateformat(this.file.day, "MM-dd") to extract the month and day part (when you don’t care about the year in your queries).

In a dataviewjs query the similar constructs would be to use toFormat("MM-dd") and compare the string output of those formatted date strings.

Also note as mentioned in the thread you linked to, that if you want to compare the dates directly (thusly demanding the same day, month and year combo), you can’t just use dv.current().file.day == page.file.cday since even equal dates are different within javascript. You would have to do something like dv.current().file.day.ts == page.file.cday.ts, or convert into ISO dates using .toISODate() or strings using date formatting as shown above.


In addition to the alternatives shown by @sanyaissues , we could then add the following variations for a query which are looking for files created at the same day and month (but ignoring the year):

```dataview
TABLE WITHOUT ID
  file.link as "Note",
  file.cday as "Date"
WHERE dateformat(this.file.day, "MM-dd") == dateformat(file.cday, "MM-dd")
```

```dataviewjs
const dayMonth = dv.current().file.day.toFormat("MM-dd")

const result = dv.pages()
  .where(p => dayMonth == p.file.cday.toFormat("MM-dd"))
  .map(p => [p.file.link, p.file.cday])

if ( result.successful )
 dv.table(["Note", "Date"], result.values )
else
  dv.paragraph("~~~~\n" + result.error + "\n~~~~")
```

Do note that all of these solutions depend on the creation date of the files to be correct, which could be a faulty assumption depending on file system, backup/restore, copy operations… So in general I would recommend to use templates to set the creation date as a property within your files, and rather use that for queries like this. That would also allow for changing the creation date to be the logical date you want, i.e. if you write the daily note of yesterday to day…

2 Likes

That’s a possibility I hadn’t considered. It rarely happens since I back up my stuff pretty frequently but never say never.

Thank you @sanyaissues this worked! I could swear I tried that querying on that property at the start but I didn’t have nearly as many notes back then so maybe I mis-read the results while testing it.

Welp, … it only took me twenty minutes to see this IS what I asked for but it’s not going to work the way I thought it would. @holroy was right, but not in the way I first thought. If I were to open a new daily note today for a date in the future in order to assign a task, that note’s file creation day would be today and not the date in it’s title. it won’t show up in the “On This Day” results even though the date in the title is today.

This will wind up being more code than I thought :thinking:

1 Like

As @holroy said, you can set the creation date as a property:

# 2024-01-20
---
creation-date: 2024-01-20
---

and then

```dataview
TABLE WITHOUT ID
  file.link as "Note",
  creation-date as "Date"
WHERE dateformat(this.file.day, "MM-dd") == dateformat(creation-date, "MM-dd")
```
1 Like

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