# Calculated dates in queries (templates)

**URL:** https://community.silverbullet.md/t/calculated-dates-in-queries-templates/136
**Category:** General
**Created:** [February 11, 2024, 3:40pm UTC](https://community.silverbullet.md/t/calculated-dates-in-queries-templates/136 "2024-02-11T15:40:32Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![gorootde](https://community.silverbullet.md/user_avatar/community.silverbullet.md/gorootde/32/124_2.png) [@gorootde](https://community.silverbullet.md/u/gorootde)
#### Post date: [February 11, 2024, 3:40pm UTC](https://community.silverbullet.md/t/calculated-dates-in-queries-templates/136/1 "2024-02-11T15:40:32Z")

</div>

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

e.g.

````markdown

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

````

---

<div class="post-metadata">

### Author: ![zef](https://community.silverbullet.md/user_avatar/community.silverbullet.md/zef/32/7_2.png) [@zef](https://community.silverbullet.md/u/zef)
#### Post date: [February 11, 2024, 4:08pm UTC](https://community.silverbullet.md/t/calculated-dates-in-queries-templates/136/2 "2024-02-11T16:08:24Z")

</div>

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](https://silverbullet.md/Space%20Script) 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](https://silverbullet.md/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](https://tc39.es/proposal-temporal/docs/)) 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](https://youtu.be/ZiM1RM0DCgo?si=OXSPNlQq9tz9ZT_O)

Hope that helps.

---

<div class="post-metadata">

### Author: ![Maarrk](https://community.silverbullet.md/user_avatar/community.silverbullet.md/maarrk/32/56_2.png) [@Maarrk](https://community.silverbullet.md/u/Maarrk)
#### Post date: [February 11, 2024, 6:32pm UTC](https://community.silverbullet.md/t/calculated-dates-in-queries-templates/136/3 "2024-02-11T18:32:51Z")

</div>

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

````auto
```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](https://github.com/silverbulletmd/silverbullet/issues/691)~~:

```auto
{{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.

---

<div class="post-metadata">

### Author: ![gorootde](https://community.silverbullet.md/user_avatar/community.silverbullet.md/gorootde/32/124_2.png) [@gorootde](https://community.silverbullet.md/u/gorootde)
#### Post date: [February 18, 2024, 8:13pm UTC](https://community.silverbullet.md/t/calculated-dates-in-queries-templates/136/4 "2024-02-18T20:13:08Z")

</div>

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](https://github.com/gorootde/silverbullet-collection)

---

<div class="post-metadata">

### Author: ![paletochen](https://community.silverbullet.md/letter_avatar_proxy/v4/letter/p/cdc98d/32.png) [@paletochen](https://community.silverbullet.md/u/paletochen)
#### Post date: [February 18, 2024, 9:43pm UTC](https://community.silverbullet.md/t/calculated-dates-in-queries-templates/136/5 "2024-02-18T21:43:49Z")

</div>

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

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

````

Where `Library/Personal/Query/Person` is your template

```auto
---
tags: template
---

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

```

---

<div class="post-metadata">

### Author: ![gorootde](https://community.silverbullet.md/user_avatar/community.silverbullet.md/gorootde/32/124_2.png) [@gorootde](https://community.silverbullet.md/u/gorootde)
#### Post date: [February 18, 2024, 10:40pm UTC](https://community.silverbullet.md/t/calculated-dates-in-queries-templates/136/6 "2024-02-18T22:40:25Z")

</div>

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

---

<div class="post-metadata">

### Author: ![paletochen](https://community.silverbullet.md/letter_avatar_proxy/v4/letter/p/cdc98d/32.png) [@paletochen](https://community.silverbullet.md/u/paletochen)
#### Post date: [February 18, 2024, 10:48pm UTC](https://community.silverbullet.md/t/calculated-dates-in-queries-templates/136/7 "2024-02-18T22:48:51Z")

</div>

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!

---

<div class="post-metadata">

### Author: ![damufo](https://community.silverbullet.md/user_avatar/community.silverbullet.md/damufo/32/179_2.png) [@damufo](https://community.silverbullet.md/u/damufo)
#### Post date: [November 24, 2024, 10:33am UTC](https://community.silverbullet.md/t/calculated-dates-in-queries-templates/136/10 "2024-11-24T10:33:00Z")

</div>

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.~~

---

<div class="post-metadata">

### Author: ![Marco10x15](https://community.silverbullet.md/user_avatar/community.silverbullet.md/marco10x15/32/432_2.png) [@Marco10x15](https://community.silverbullet.md/u/Marco10x15)
#### Post date: [November 24, 2024, 11:53am UTC](https://community.silverbullet.md/t/calculated-dates-in-queries-templates/136/11 "2024-11-24T11:53:09Z")

</div>

I’ve a space-script that can do what you want.

see: [Space Script: addTimeToDate](https://community.silverbullet.md/t/space-script-addtimetodate/261).
