RRULE BYDAY and BYMONTHDAY: nth weekday and last day rules
One-line answer: How to express the second Tuesday, the last Friday, or the 1st and 15th with RFC 5545 recurrence rules.
RRULE's real strength is BYDAY and BYMONTHDAY. They turn “the second Tuesday of every month” or “the last day of the month” into one line.
Two forms of BYDAY
| Form | Meaning | Example |
|---|---|---|
MO | Every Monday | FREQ=WEEKLY;BYDAY=MO |
2TU | Second Tuesday of the month | FREQ=MONTHLY;BYDAY=2TU |
-1FR | Last Friday of the month | FREQ=MONTHLY;BYDAY=-1FR |
The numeric prefix means “the nth in the month” for MONTHLY and YEARLY rules. Negative values count from the end: -1 is last, -2 is second to last. WEEKLY rules do not use the prefix.
BYMONTHDAY
BYMONTHDAY=1,15 means the 1st and 15th of each month. BYMONTHDAY=-1 is the last day, -2 the day before. You never have to worry about months without a 31st: nonexistent dates simply do not occur.
Together they intersect
FREQ=MONTHLY;BYMONTHDAY=13;BYDAY=FR occurs only on a 13th that is also a Friday: an intersection, not a union. If you want either the 13th or every Friday, you need two rules. Note this is the opposite of cron's OR rule, which is why conversions get it wrong.
Worked examples
Second Tuesday FREQ=MONTHLY;BYDAY=2TU
Last Friday FREQ=MONTHLY;BYDAY=-1FR
1st and 15th FREQ=MONTHLY;BYMONTHDAY=1,15
First Monday quarterly FREQ=MONTHLY;INTERVAL=3;BYDAY=1MO
Last Thursday of Nov FREQ=YEARLY;BYMONTH=11;BYDAY=-1TH
“The last business day” is hard to express with RRULE alone: BYMONTHDAY=-1 still lands on a weekend if the month ends on one. Holidays and weekends need EXDATE entries or application logic.
What to check
After building a rule, look at twelve months of occurrences. February, 30-day months and year-end expose boundary mistakes quickly. CronPulse shows the next ten and exports an .ics you can open in a calendar.
Paste an expression and see its next run times right away.
Open CronPulseFrequently asked questions
Is `BYDAY=2TU` the second Tuesday of the week or of the month?
It depends on FREQ. With FREQ=MONTHLY or YEARLY it is the second Tuesday of the month. WEEKLY rules do not take the numeric prefix.
Does BYMONTHDAY=-1 work in February?
Yes. It resolves to the actual last day of that month: 28 in a common year and 29 in a leap year.
Sources
Related reading
- Cron in five fields: minute, hour, day, month, weekdayWhat each of the five Unix cron fields means, which values are allowed, and how to read an expression without guessing.
- Cron special characters: *, /, -, commas, ?, L, W and #The eight symbols that make cron expressive, what each one means, and where implementations disagree.
- Five-field Unix cron versus six-field Quartz and EventBridgeSeconds, weekday numbers, the ? rule and the year field: what changes when you move a schedule between cron dialects.
- Daylight saving and cron: the day 02:30 disappearsHow clock changes affect cron runs, and how to move jobs out of the 2–3 AM danger window.