I asked “write javascript for n8n code node that tells me if it is the 4th Wednesday of the month” and it generated the code below (which worked when I commented out line 1):
const { DateTime } = require("luxon");
// Get the current date
const now = DateTime.now();
// Check if today is the 4th Wednesday of the month
if (now.weekday === 3 && now.day > 22 && now.day <= 28) {
// Today is the 4th Wednesday of the month
return { message: "Today is the 4th Wednesday of the month" };
} else {
// Today is not the 4th Wednesday of the month
return { message: "Today is not the 4th Wednesday of the month" };
}
I then asked “Update the script so I can use it in future months and years”
const { DateTime } = require("luxon");
// Get the current date
const now = DateTime.now();
// Calculate the 4th Wednesday of the month
const year = now.year;
const month = now.month;
const firstDayOfMonth = DateTime.local(year, month, 1);
const firstWednesday = firstDayOfMonth.plus({ days: (3 - firstDayOfMonth.weekday + 7) % 7 });
const fourthWednesday = firstWednesday.plus({ weeks: 3 });
// Check if today is the 4th Wednesday of the month
if (now.toISODate() === fourthWednesday.toISODate()) {
// Today is the 4th Wednesday of the month
return { message: "Today is the 4th Wednesday of the month" };
} else {
// Today is not the 4th Wednesday of the month
return { message: "Today is not the 4th Wednesday of the month" };
}
It seems to work, but as a non-coder I’m not 100% sure!
Does it make sense?