fantasycalendar / obsidian-fantasy-calendar

MIT License
175 stars 7 forks source link

Calculate Age Formula? #61

Open ProStashio opened 2 years ago

ProStashio commented 2 years ago

Is there any code or formula that can be used to calculate someone's age?

Let's say I want to input someone's Birthday for my notes, I would love it to show someone's age. So if I come back to them in an in-game year it would show they are a year older.

ckennedy666 commented 1 year ago

My solution uses the CustomJS + Dataview plugins in order to execute the following code:

getBirthday(dv) {
    const fcdate = app.plugins.getPlugin("fantasy-calendar").defaultCalendar.current;
    const pg = dv.current();
    let age = null
    if (pg.Birthday) {
        const bd = pg.Birthday.split("-");
        age = fcdate.year - bd[0] - ((bd[1] > fcdate.month && bd[2] > fcdate.day) ? 1 : 0);
    }
   dv.span(age);
}

The above requires a YAML field called Birthday: to be present, and using dates in the same format as Fantasy Calendar.

You will need to follow the instructions for CustomJS and put the above into a class file. For the purposes of the below, the class file is called tDates.js. You also have to allow inline dataviewjs. Insert the following into your document where you want the age calculated:

Age: `$= const {tDates} = customJS; tDates.getBirthday(dv);`