Calculated dates in queries (templates)

How would I do date calculations within queries? Was not able to find any examples in the documentation.


# Tasks due in next seven days
tasks where deadline >= "{{today}}" and deadline <= "{{today}}+7d"

We had a discussion on this in Discord yesterday, let me quickly replicate what I said there (we should really stop talking content there :wink: ):

In the past there was no good way to do this in SilverBullet. As a stop gap I introduced a few super ad-hoc date functions like today() and nextWeek() — these happen to be what you’re asking for but… that’s just lucky. So the brief answer is where deadline >= today() and deadline <= nextWeek() should work.

That said, what actually triggered implementing Space Script was to not implement and design a very elaborate date function library myself, but allowing users to do this — and ideally share the result back as Libraries

Technically this is feasible now, now somebody just has to do it.

In space script, you already have access to the temporal API (Temporal documentation) via a polyfill. So what you can do is define custom functions using this API. Of course, for this you need to be a bit comfortable with JavaScript. And of course, you don’t have to design a full date library and can just add whatever functions you need. In this video I demo some of the template stuff and at the very end also show how can go about using space script:

Hope that helps.

1 Like

I wanted to have this to add previous and next buttons to my weekly notes, and came up with this snippet:

silverbullet.registerFunction("addDays", (date, days) => {
  const plainDate = Temporal.PlainDate.from(date);
  const offsetDate = plainDate.add(Temporal.Duration.from({ days: Number(days) }));
  return offsetDate.toString();

You can use it like this, unless you want to pass a negative number:

{{addDays(today, 7)}}

Once I have a few more things I think I would publish with a Library, since I’m already running six SiverBullet instances for family and friends.


For those reading this later: I meanwhile created some time arithmetics scripts and shared them in my repo: GitHub - gorootde/silverbullet-collection: Collection of awesome stuff


These are super cool scripts.
I already imported to my instance the ones calculating the days.

By the way, would you know how to sort by daysTillBirthday?
I’d love to have at the top the ones closer to their birthday.

So far I have a simple query showing the list of people

page where type = "people" and birthday != null render [[Library/Personal/Query/Person]]

Where Library/Personal/Query/Person is your template

tags: template

- [[{{ref}}|{{name}}]] 🎂 {{daysTillBirthday(birthday)}} days

Sure, I also do that by adding order by daysTillBirthday(birthday) to my query.

I tried that before but it didn’t work.
It seems that I need to add it after the render command, and not before, where I guess I tried it before.