I’m trying to set my daily notes template to show some tasks that happens only in certain week days of every month, like a meeting that happens every 1st wednesday of every month,
or a task that happens on alternate mondays.
Things I have tried
I tried templater and moment.js documentation, but didn’t find any clue.
I was hopping for something like:
<%* if (tp.date.now(“dddd”) == “monthfirstmonday”) { -%>
task 1
task 2
<%} else if (tp.date.now(“dddd”) == “alternatetuesdays”) { -%>
<%} -%>
(I know this code doesn’t work, its just to show how I was thinking about the issue)
To be honest , I think it would be easier to use the Tasks plugin which supports recurring tasks …
It should allow you to query the relevant tasks in the appropriate daily notes …
I don’t use the plugin but reading your request, this is the first thought that came to my mind …
I still have a Templater snippet for the “first monday of the month” though …
<%*
const today = moment(tp.file.title, "YYYY-MM-DD");
const weekIndex = [0, 1];
const firstMonday = weekIndex.map(
i => moment(today).startOf('month').startOf('isoWeek').add(i, 'w')
).find(monday => moment(monday).month() === moment(today).month());
const firstMondayTasks = [
`- [ ] Task 1`,
`- [ ] Task 2`,
`- [ ] Task 3`
];
if (moment(today).isSame(moment(firstMonday))) {
tR += firstMondayTasks.join("\n")
} else {
tR += `Sorry, this is not the first Monday of the month.` + "\n" + `It's just a ${moment(today).format("dddd")}`
}
%>
<% tp.file.cursor() %>
… Which, when applied to a daily note (titled following the format YYYY-MM-DD. You might need to adapt the format used in moment() for the constant today) either output:
a fake list of tasks (e.g.: if the daily is the one for 2024-02-05)
or
the sentence “Sorry, this is not the first Monday of the month. It’s just a weekdayWhatever” ()
I’m no JS expert though so there might be other/better ways to get to a similar result .
I didn’t had the time to look into “alternate tuesdays” for which I can’t really envision a solution at the moment …
Not saying that it wouldn’t possible but I can’t see how .
Ahahah ! I have no idea why I didn’t think about that …
Thanks Holroy !
Here’s an updated version of the Templater snippet I shared earlier which output a list of tasks on the 1st Monday of the month and on Tuesdays if the isoWeek number of today is even:
<%*
const today = moment(tp.file.title, "YYYY-MM-DD");
// 1st Monday of the month things
const weekIndex = [0, 1];
const firstMonday = weekIndex.map(
i => moment(today).startOf('month').startOf('isoWeek').add(i, 'w')
).find(monday => moment(monday).month() == moment(today).month());
const firstMondayTasks = [
`- [ ] Task 1`,
`- [ ] Task 2`,
`- [ ] Task 3`
];
if (moment(today).isSame(moment(firstMonday))) {
tR += firstMondayTasks.join("\n") + "\n"
}
// Alternate Tuesdays things
const alternateTuedayTasks = [
`- [ ] Task 4`,
`- [ ] Task 5`,
`- [ ] Task 6`
];
// There's no isEven method in JS: (number % 2 == 0) can be use instead
// To output the list of tasks on odd isoWeeks number use: !(moment(today).isoWeek() % 2 == 0)
if ((moment(today).isoWeek() % 2 == 0) && moment(today).format("dddd") == "Tuesday") {
tR += alternateTuedayTasks.join("\n") + "\n"
}
-%>
<% tp.file.cursor() %>