Gallery
Kuovonne's Guide to Scripting in Airtable
Share
Explore
Free Scripts

2nd & 4th Monday of each Month

Your donations tell Kuovonne that you value her content and want her to continue publishing.

Inspiration

The inspiration for this script came from a .
Someone wanted to schedule an automation to go out on the 2nd and 4th Monday of each month. While you can schedule an Airtable automation to run every two weeks, that solution won’t work because the schedule will be off as soon as a month has five Mondays in it.
I gave two suggestions ...
I can think of two approaches:
(Option 1) Schedule the automation to run every Monday. Have a scripting action that calculates if it is the 2nd or 4th Monday and outputs the result. Have rest of the automation in a condition based on the script output.
(Option 2). Have a control table with two records. One record for the 2nd Monday of the Month, and another record for the 4th Monday of the month. Use a formula field to calculate the actual date. Use a “when record meets conditions” trigger.
This script discusses how to write the script in Option 1.

Discussion

First get the current date.
const date = new Date()
We don’t need to get the day of the week because the automation is scheduled to run on Mondays.
However, we do need to know the day of the month to determine which Monday it is in the month (1st, 2nd, 3rd, 4th, or 5th)
const dayOfMonth = date.getDate()
If the day of the month is 1-7, it must be the first Monday of the month. If the day of the month is 8-14, it must be the second Monday of the month. And so on.
let dayOfWeekOcurrance
if (dayOfMonth <= 7) {
dayOfWeekOcurrance = 1
} else if (dayOfMonth <= 14) {
dayOfWeekOcurrance = 2
} else if (dayOfMonth <= 21) {
dayOfWeekOcurrance = 3
} else if (dayOfMonth <= 28) {
dayOfWeekOcurrance = 4
} else {
dayOfWeekOcurrance = 5
}
However, that long else if chain is a bit unwieldy. A shorter way is to divide by 7 and round up to a whole number.
const dayOfWeekOcurrance = Math.ceil(dayOfMonth / 7)
Finally, output the value to be used in a future automation action.
output.set("dayOfWeekOcurrance", dayOfWeekOcurrance)
Here is the full script.
const date = new Date()
const dayOfMonth = date.getDate()
const dayOfWeekOcurrance = Math.ceil(dayOfMonth / 7)
output.set("dayOfWeekOcurrance", dayOfWeekOcurrance)
You can now use the output of that script in a condition group later in the automation.
image.png
The content in this guide is free, but creating it takes time and money. If you like this content, .

Share
 
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.