How would I do date calculations within queries? Was not able to find any examples in the documentation.
e.g.
# Tasks due in next seven days
```query
tasks where deadline >= "{{today}}" and deadline <= "{{today}}+7d"
How would I do date calculations within queries? Was not able to find any examples in the documentation.
e.g.
# Tasks due in next seven days
```query
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 ):
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: https://youtu.be/ZiM1RM0DCgo?si=OXSPNlQq9tz9ZT_O
Hope that helps.
I wanted to have this to add previous
and next
buttons to my weekly notes, and came up with this snippet:
```space-script
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 silverbullet.md 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
```query
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.
Interesting,
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.
Thanks!
Hello, I see than nextWeek() is implemented, but not is in documentation.
Also, implement a function built-in addDays() wil be wonderfull.
Thinking well I don’t think it’s necessary, sorry:
Also, implement a function built-in addDays() wil be wonderfull.
I’ve a space-script that can do what you want.