Inline Query to find note between two dates

Not sure why you want this as a inline query, but here is the script transformed to be an inline query. I had to transform away from the string template literals, `…`, to ordinary string concatenations.

`$= 
const myDate = moment(dv.current().file.name, 'YYYY-MM-DD');
const house = await dv.pages('')
  .filter(p => p.file.path.includes('Calendar/Houses'))
  .where(p => p.type == "House" 
    && p["Date-Moved-In"] <= myDate 
    && p["Date-Moved-Out"] >= myDate)
  .file.link;
if (house.values.length == 0) {
  dv.paragraph("Were you homeless on " + myDate.format('YYYY-MM-DD') + "?") 
} else if ( house.values.length == 1 ) {
  dv.paragraph("On " + myDate.format('YYYY-MM-DD') + " you lived in " +  house[0] + ".") 
} else {
 dv.header(3, "Which one did you really live in?");
 dv.list(house)
}`

Do note that there are some changes to the actual DOM elements which are output from dataview when you change to/from inline or ordinary queries. In some cases, this can affect styling options and how the final result is rendered.

Lastly, this is a fairly complex query, which indicates it might be needed to change it every now and then, and for that purpose I would look into using dv.view() for inclusion in your notes, so that you’ll not end up with having to change a bunch of files because of some of these future changes.

Bonus tip: How to do dv.view()

In case your query is to be included into a template, like a daily note template, it’s often better to call a dv.view() from the template/daily note. This allows for the daily note to get a fixed reference to call the query, and allows for future you to change the query if needed.

To do this change you need to have a folder to hold the query in, like vault/_js, and you then need to create a file like myView.js in that folder. This will then hold your javascript query.

In place of your original query, you can now insert the following:

```dataviewjs
dv.view("_js/myView")
```

If your original query was an ordinary DQL query, you could transform that into a dataviewjs query by doing:

await dv.tryMarkdownQuery(`
... your original query ...
`)

A dv.view() will behave as if it were in the original file, so stuff like dv.current() and field references, will refer to the original file where you called dv.view(). For more information see: Codeblock Reference - Dataview

Finally, dv.view(), also allows for some custom CSS to be included, if instead of vault/_js/myView.js you create vault/_js/myView/view.js and vault/_js/myView/view.css

2 Likes