Using ChatGPT to generate javascript for code node

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?

Short answer

To get this to run in the Code node, you’ll need to remove the first line (where you require Luxon) Luxon is automatically available in n8n already.

Once I remove that line, and ran the code in the Code node, it worked (delightfully, today is the 4th Wednesday of the month)

A bit more detail

As you mentioned you’re not a coder, you may be wondering what Luxon is. It’s a JavaScript library to make working with date and time easier. Luxon website if you want to check it out.

Luxon in n8n provides a lot of utility: more info in the docs

If you want to, you can simplify your code a little more: n8n uses Luxon to provide two custom variables, including $now, which is equivalent to DateTime.now(). So your final code could look like this:

// 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" };
}
2 Likes

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