fantasycalendar / obsidian-fantasy-calendar

MIT License
175 stars 7 forks source link

[Feature Request]: Api options for getting date as number and formatted string #118

Closed JimWolff closed 1 year ago

JimWolff commented 1 year ago

Is your feature request related to a problem? Please describe. I'd love to be able to use this plugin for templating more, by having more api calls available. When creating some documents, i have various values that i'd like to get from the calender and put them into my notes, either as variables themselves, like the number of days elapsed since start, or number or formated string versions of the current or selected date. This is because when templating things like encounters, events, timelines and so on, it would be great to write the day or a full text for the date into those, like 1st of Zarantyr, 998. or Day 235.

Describe the solution you'd like An: api.daysSinceStart() could make it easy to template: "Day x" into your notes, it could also provide me with a way to template something that happened api.daysSinceStart()+3 or similar.

An: api.currentDateString(), api.currentDateString("ddd MMM, yyyy), and maybe also with days since start inputs like this: api.currentDateString(42), api.currentDateString(42,"ddd MMM, yyyy). Could make it very easy to write out a certain date tin a note like: 1st of Zarantyr, 998 or similar but based on an input number.

I often add timeline blocks as we go with a title of what the event is, and a subtitle with the number of days from start and the pretty date string.

valentine195 commented 1 year ago

Hey, these two options can be done through the calendar helper. I don’t have any typings set up for that outside the helper file itself though (which is too big), and helpers are recreated when you request them instead of being singletons per calendar. Both of this will change in an upcoming rewrite (TBD when) but it’s usable now.

Here is how you get a helper: https://github.com/fantasycalendar/obsidian-fantasy-calendar/blob/2d504495f69219899358adae788965c7eafa7ae7/src/api/api.ts#L165

Formatted current date: https://github.com/fantasycalendar/obsidian-fantasy-calendar/blob/2d504495f69219899358adae788965c7eafa7ae7/src/helper/index.ts#L523

the day number is a little harder. You can get raw number days from the beginning of time (day 1 year 1) with this https://github.com/fantasycalendar/obsidian-fantasy-calendar/blob/2d504495f69219899358adae788965c7eafa7ae7/src/helper/index.ts#L915 but if you want a days between you’ll have to do that for both dates and subtract them.

JimWolff commented 1 year ago

Thank you for the quick response, I did look around the code to see if i could find something, but must have been looking in the wrong place, I'll try and give this a go and see if i can create a note template that pulls some of this out in a nice way, if you dont mind the issue being open i can post an example here if i figure it out, otherwise feel free to close the issue. 😀

JimWolff commented 1 year ago

This is what i ended up doing in Templater in obsidian, for context I'm using the ebberon standard calendar with the default campaign start of {day: 1, month: 0, year: 998} which is Zarantyr 1st, 998

I have this codeblock in my template:

<%*
let h = FantasyCalendarAPI.getHelper()
let csd = {day: 1, month: 0, year: 998} //campaignStartData
let niceDate = h.currentDate
let totalDaysBeforeCampaignStart = h.totalDaysBeforeYear(csd.year) + h.daysBeforeMonth(csd.month)+csd.day-1
let totalDaysSinceNow = (h.totalDaysBeforeYear(h.current.year) + h.daysBeforeMonth(h.current.month)+h.current.day-1)
let currentTotalDays = totalDaysSinceNow-totalDaysBeforeCampaignStart+1
let niceDayAndDate = "Day " + currentTotalDays + " - " + niceDate
-%>

which then makes it possible to have something like: <% niceDayAndDate %> at the top of my session notes which becomes: Day 38 - Olarune 10th, 998

Or automatically added as an "encountered: <% h.currentDate %>" making it: encountered: Olarune 10th, 998