How to find a list of notes dated within a fixed period relative to the current date?

What I’m trying to do

I am trying to write a query that will turn out notes that look back over a fixed period relative to the current date.

For example, I’d like a note that brings up only the last two weeks of my daily notes, or the last month. I don’t know how to reference the current date variable, let alone put a comparator and a date range in a search query.

Things I have tried

  • learning about search syntax from Obsidian Help files
  • experimenting
  • searching this forum
1 Like

The Vantage plugin does this.

Nice one, @ryanjamurphy, thank you.

EDIT: Now that I have tried them, your plugin, along with Argentina’s, are very helpful in translating a specific date range into an embedded query, but I don’t see how to embed a relative date range that automatically updates to show, say, only the notes for the last X weeks.

In the embedded query resulting from Vantage and Natural Language Dates, ‘today’ is translated as the specific date on which the query is built. The other dates in the range are also listed in specific form, so the embedded query is static. I don’t yet see a way to embed a search referencing a fixed period relative to the current date variable.

To put it another way, rather than a date range specified by specific ‘start’ and ‘end’ dates, I am looking for a range specified by the variable date ‘today’ and a fixed ‘time period’. I don’t want ‘today’ and ‘last week’ to be hard coded as specific dates in the query itself, but translated each time I access the note, so that the note always lists, say, the last week’s notes whatever day I look at it. Am I missing something?

1 Like

Do you want to access the notes which were created, say in the last X weeks or do you want to access notes which were visited in the last X weeks. The latter being like a web browser’s history?

Ah, I missed your edit, and the embed requirement. For embedded relative date search, you have to turn to Dataview (there may be other options).

Here’s what I use. Below are two dataviewjs blocks to find (1) all overdue items in my daily notes, and (2) to pick up notes with ᐤ△ in the title created in the last 14 days:

Overdue tasks

dv.taskList(dv.pages('"Calendar"')
    .where(p => p.file.day && p.file.day <= (luxon.DateTime.now().minus({days: 1})))
	.sort(b => b.file.day)
    .file.tasks
    .where(t => !t.completed), true)

New projects

dv.list(dv.pages().where(p => (p.file.name.contains("ᐤ△") && p.file.ctime >= luxon.DateTime.now().minus({days:14}))).file.link)
1 Like

Hi @Walrux, I am looking to show files (within a certain folder) to which I have explicitly given a date or timestamp that falls within the last X weeks.

Hi @ryanjamurphy, thanks for getting back to me.

The Dataview solution that you have suggested and exemplified looks very promising. I am not yet familiar with dataviewjs but I can see what you are doing. I will now look into it, and thank you for your help.

1 Like
table file.ctime from "Folder"
where file.ctime >= date(2021-08-21)-dur(7 day)
where file.ctime < date(2021-08-22)
sort file.ctime desc

OR

table file.day from "Folder"
where file.day >= date(2021-08-21)-dur(7 day)
where file.day < date(2021-08-22)
sort file.day desc

Using this query in dataview will help me display notes created( flie.ctime) or notes on which I have entered date (file.day) spanning from 14th August to 21st August. This I think will suffice your need for “today” to be a variable which you can interchange with whichever date you want. As a result of which you can choose a time span according to your needs.

I am not 100% sure that this is what you are looking for but as far as I can understand this should be it.

1 Like

Note the luxon.DateTime calculations in my example above. Instead of writing a fixed date into the script, you can make it relative to the current date.

(If you’re using these dataviews in a template, it may be better to insert the fixed date via a variable in the template, such that looking at the dataview for August 26 2021 will always show data relative to that date.)

Thank you for your examples, @Walrux, I am looking to encode a query that references that last X weeks but does not need any further input, hence the utility of a current date variable.

Yes, @ryanjamurphy, I noticed the luxon.DateTime.now() property and recognised it as the current date variable that I was looking for. Thank you.

1 Like

Hi @ryanjamurphy, I am not used to dataviewjs so could you tell me if date(today) in dataview is similar to luxon.DateTme.now() in dataviewjs ?

1 Like

Would something along the line of one of these work?

LIST 
WHERE date(today) - file.mday <= dur(7 days)
SORT file.mday desc
TABLE file.mday AS "This Week"
WHERE date(today) - file.mday <= dur(7 days)
SORT file.mday desc

See: Data Annotation - Dataview

Angel

1 Like

I think it’s similar, yeah! I don’t know enough about the inner workings to describe any differences.

1 Like

Thank you, Angel, for your solution and the reference link. Very helpful.

A pleasure, Nifty. Good to hear you have got things working as you want.

I have tweaked my queries:

  1. To use SORT file.mtime desc, as mtime gives a more granular time-based sort that brings the most-recent changes to the top of the query, which helps if a lot of files have been edited within the set time frame
  2. Added WHERE file.name != this.file.name, which instructs the query to not to list the self-referencing file name that the query is in
LIST 
WHERE date(today) - file.mday <= dur(7 days)
WHERE file.name != this.file.name
SORT file.mtime desc
TABLE file.mday AS "This Week"
WHERE date(today) - file.mday <= dur(7 days)
WHERE file.name != this.file.name
SORT file.mtime desc

Angel

These are interesting tweaks, @Angel, thanks for sharing.

I’m just starting to use the Dataview query language. The full language reference documentation is currently inaccessible (the link for it is showing ‘404 File not found’), but I have now set up a basic query that will work for me using an ISO 8061 date tag (I haven’t got other date formats to work.)

Thanks again to @ryanjamurphy, @Walrux, @Angel, all of whom responded to this post and helped me to answer my query. I wish you all a good Sunday.

1 Like

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