What I’m trying to do
I’m using the “Metadata Menu” plugin.
I have a note field, Date_of_Birth, which represents a person’s birthday.
I want to configure another field, Days_Before_Birthday, to display the number of days remaining until the next birthday.
I can use a formula field that accepts JavaScript, but my code doesn’t work.
Things I have tried
This code work in a dataviewjs block inside the note:
var startDate = DateTime.fromISO(dv.current().Date_of_Birth).set({
year: DateTime.now().get("year")
});
var diff = startDate.diffNow().as('day');
if(diff < 0 ) {
startDate = DateTime.fromISO(dv.current().Date_of_Birth).set({
year: DateTime.now().get("year") + 1});
diff = startDate.diffNow().as('day');
}
const diffAbs = Math.abs(diff);
const wholeNumberedDateDifference = Math.round(diffAbs);
dv.span("Birthday is in: " + wholeNumberedDateDifference + " days");
I tried this code in the formula property:
var startDate = DateTime.fromISO(dv.current().Date_of_Birth).set({
year: DateTime.now().get("year")
});
var diff = startDate.diffNow().as('day');
if(diff < 0 ) {
startDate = DateTime.fromISO(dv.current().Date_of_Birth).set({
year: DateTime.now().get("year") + 1});
diff = startDate.diffNow().as('day');
}
const diffAbs = Math.abs(diff);
const wholeNumberedDateDifference = Math.round(diffAbs);
return wholeNumberedDateDifference;
The field stay empty.