Templater: how can I show tasks in specific days of specific weeks (like: on first monday of every month, or on alternate tuesdays)?

What I’m trying to do

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 :sweat_smile:, I think it would be easier to use the Tasks plugin which supports recurring tasks :blush:

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 :sweat_smile:

I still have a Templater snippet for the “first monday of the month” though :blush:

<%*
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” (:innocent:)

I’m no JS expert though so there might be other/better ways to get to a similar result :blush: .

I didn’t had the time to look into “alternate tuesdays” for which I can’t really envision a solution at the moment :pensive:
Not saying that it wouldn’t possible but I can’t see how :sweat_smile: .

1 Like

The easiest way is checking the week number, and decide whether you want your thing to happen on weeks with an odd or even number.

2 Likes

Ahahah :sweat_smile: ! I have no idea why I didn’t think about that :woman_facepalming: :innocent:

Thanks Holroy :blush: !

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() %>
2 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.