opyebCronPulse

RRULE BYDAY and BYMONTHDAY: nth weekday and last day rules

RRULE · 6 min read

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

FormMeaningExample
MOEvery MondayFREQ=WEEKLY;BYDAY=MO
2TUSecond Tuesday of the monthFREQ=MONTHLY;BYDAY=2TU
-1FRLast Friday of the monthFREQ=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 CronPulse

Frequently 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