I cant seem to figure it out myself.
I want to make a list with birthdays of people and want to sort it based on the calculated days until the next Birthday, similar to this:
Name | Birthday | Age | Next Birthday (sort asc) |
---|---|---|---|
John | 1984-01-01 | 41 | 1 days |
Mary | 2000-02-14 | 24 | 45 days |
Peter | 1986-05-01 | 38 | 121 days |
… | … | … | … |
I have the names and birthday attributes in a #person codeblock and i also have the scripts to CalculateAge
and DaysUntilNextBirthday
which then I added to the template to create the list.
Until now everything is fine. But what i have trouble with is to sort the list based on the next birthday. because it’s not an active attribute but rather a caculated value in the table template.
name: Mary
birthday: 2000-02-14
---
name: Peter
birthday: 1986-05-06
---
name: John
birthday: 1996-06-13
---
name: Anna
birthday: 1991-12-17
---
name: Jim
birthday: 1983-09-28
---
name: Susan
birthday: 1987-07-10
The space-script to calculate the Age and DaysUntilNextBDay:
silverbullet.registerFunction("daysTillBirthday",(date) => {
const birthDate = Temporal.PlainDate.from(date)
const now = Temporal.Now.plainDateISO();
const thisYearBirthday = new Temporal.PlainDate(now.year, birthDate.month, birthDate.day);
if (now.equals(thisYearBirthday)) {
return 0; // Happy Birthday!
} else if (Temporal.PlainDate.compare(now,thisYearBirthday) < 0) {
// Birthday is still coming this year
return now.until(thisYearBirthday, { largestUnit: 'days' }).days;
} else {
// Birthday has passed for this year, calculate for next year
const nextYearBirthday = new Temporal.PlainDate(now.year + 1, birthDate.month, birthDate.day);
return now.until(nextYearBirthday, { largestUnit: 'days' }).days;
}
})
export function calcAge(birthday) {
const birthDate = new Date(birthday);
const today = new Date();
let age = today.getFullYear() - birthDate.getFullYear();
const monthDiff = today.getMonth() - birthDate.getMonth();
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
The list-template saved in “Library/Personal/Templates/BirthdayTable”:
---
tags: template
displayName: "Birthdays Table Template"
description: "Template to output the Birthday Table"
---
| [[{{name}}]] |{{niceDate(birthday)}} | {{dateDiff(niceDate(birthday), today(),"years")}}| {{daysTillBirthday(niceDate(birthday))}}|
and at last the Template for the list:
| Person | Birthday | Age | Days |
|--------|----------|-----|------|
{{{person where page = "birthdays_test" render [[Library/Personal/Templates/BirthdayTable]] order by "Days" asc}}}
what i get is a nice liste, exactly as i wanted but it’s not sorted in any way:
If i sort by birthday
or name
it works without issues, because these are the initial attributes from the query. but when I try to sort after "Days"
or "Age"
it doesnt work, probably because these are calculated through the template, and are not attributes of the person
.
I cant figure it out myself how to create a variable attribute and assign it to that person.
I tried to assign using a @nextbd variable and declaring it wiht #let
, but it’s not working:
name: Susan
birthday: 1987-07-10
nextbirthday: {{nextbd}}
{{#let @nextbd = daysTillBirthday(niceDate(@person.birthday))}}
{{/let}}
i’m sure it’s somthing wrong with the syntax or something else. Could someone please help me with this one? Been trying for the last couple of hours, but nothing seems to work. i’m sure missing something…
Thanks in advance.