Dataviewjs: limiting the # of pages to a timeframe?

What I’m trying to do

I currently have charts showing fields from all available daily pages, but I want to limit them to only pages in the last 60 days. This is the line that gathers the pages and arranges them in the right order:

const pages = dv.pages('"Calendar/Daily Notes"').where(p => p.sleep > 0).sort(p => p.file.name, 'asc')

Things I have tried

I saw dur(7 day) in a non-js dataview response and tried to add it, but that resulted in an error:

const pages = dv.pages('"Calendar/Daily Notes"').where(p => p.sleep > 0).sort(p => p.file.name, 'asc').dur(60 day)

That’s as far as I got, I had trouble on what to search for, in javascript and how it would relate to dataviewjs

You’d include the duration part within a where clause, and it kind of depends on how you get the date from your daily notes, but something like the following should work:

```dataviewjs
const pages = dv.pages('"Calendar/Daily Notes"')
  .where(p => p.sleep > 0)
  .where(p => p.file.day >= dv.date("today") - dv.duration("60 days"))
  .sort(p => p.file.name, 'asc')

... rest of your script ...
```
Bonus tip: How to present code properly in a forum post

If you want to showcase either markdown, or code blocks, or dataview queries properly in a forum post, be sure to add one line before and one life after what you want to present with four backticks, ````. This will ensure that any other backticks (like for code blocks or queries) is properly shown.

1 Like

Thank you, both for the solution and the tip for presenting code properly here.

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