Hello Community!
Ever needed to run a workflow not on a specific day of the month but on something like the third Friday, or a few days before the first Thursday of the month?
In this video, we’ll show you how to set up a workflow that kicks off on custom dates like these, making your process automation more flexible and precise.
Here are the prompts you can use to have ChatGPT write the JavaScript code for you:
SCENARIO 1 - To run your workflow 3 days before the first Thursday of the month
ChatGPT prompt
I want to schedule a Process Street workflow to run “3 days before the first Thursday of every month” via ‘Code by Zapier’
Please note that the first Thursday of the month can be different in a different month
For this, I’ll follow the next steps:
- Step 1: Schedule by Zapier so my Zap triggers the first of every month
- Step 2: ‘Code by Zapier’ will calculate the date “3 days before the first Thursday of every month”
Note: currentDate is initialized from the inputData.date to ensure you’re starting the calculation based on the provided date.
- Step 3: ‘Delay by Zapier’ so it delays until the calculated date in Step 2
- Step 4: Run the workflow in Process Street
Please write JavaScript code that I can use for Step 2:
- Accepts an input date (as inputData.date).
- Determines the first Thursday of the month for the given date.
- Subtracts 3 days from that first Thursday.
- Returns the final calculated date in ISO format.
Make sure that:
- The code should work by creating a Date object for the input date.
- You should calculate the first Thursday of the month by checking the weekday of the 1st of the month.
- After calculating the first Thursday, subtract 3 days from it.
- Finally, return the calculated date in ISO format.
JavaScript Code
const inputDate = new Date(inputData.date); // Initialize input date from inputData
// Get the first day of the month
const firstDayOfMonth = new Date(inputDate.getFullYear(), inputDate.getMonth(), 1);
// Determine the weekday of the first day of the month (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
const firstDayWeekday = firstDayOfMonth.getDay();
// Calculate the date of the first Thursday of the month
let firstThursday = new Date(firstDayOfMonth);
// If the first day is later in the week than Thursday (i.e. day > 4), set the first Thursday to the next week
if (firstDayWeekday <= 4) {
firstThursday.setDate(firstDayOfMonth.getDate() + (4 - firstDayWeekday));
} else {
firstThursday.setDate(firstDayOfMonth.getDate() + (4 - firstDayWeekday + 7));
}
// Subtract 3 days from the first Thursday
firstThursday.setDate(firstThursday.getDate() - 3);
// Return the calculated date in ISO format
return { calculatedDate: firstThursday.toISOString() };
SCENARIO 2 - To run your workflow the third Friday of the month
Based on Scenario prompt:
ChatGPT prompt
Please write JavaScript code that I can use for Step 2:
- Accepts an input date (as inputData.date).
- Determines the third Friday of the month for the given date.
- Returns the final calculated date in ISO format.
Make sure that:
- The code should work by creating a Date object for the input date.
JavaScript Code
const inputDate = new Date(inputData.date); // Initialize input date from inputData
// Get the first day of the month
const firstDayOfMonth = new Date(inputDate.getFullYear(), inputDate.getMonth(), 1);
// Determine the weekday of the first day of the month (0 = Sunday, 1 = Monday, ..., 6 = Saturday)
const firstDayWeekday = firstDayOfMonth.getDay();
// Calculate the date of the first Friday of the month
let firstFriday = new Date(firstDayOfMonth);
// If the first day is later in the week than Friday (i.e. day > 5), set the first Friday to the next week
if (firstDayWeekday <= 5) {
firstFriday.setDate(firstDayOfMonth.getDate() + (5 - firstDayWeekday));
} else {
firstFriday.setDate(firstDayOfMonth.getDate() + (5 - firstDayWeekday + 7));
}
// Calculate the third Friday (14 days after the first Friday)
let thirdFriday = new Date(firstFriday);
thirdFriday.setDate(firstFriday.getDate() + 14); // Add 14 days to first Friday to get the third Friday
// Return the third Friday in ISO format
return { calculatedDate: thirdFriday.toISOString() };