Search code examples
calendargoogle-calendar-apiicalendar

iCal Recurring monthly event on the closest weekday possible?


I would like to create a recurring event on the 24th of every month and if the day falls on a weekend, schedule it for the closest weekday. If the day falls on a Saturday, I would like to schedule it for the previous weekday (Friday). If the day falls on a Sunday, I would like to schedule it for the next weekday (Monday). Is this something that is possible to do with iCal RRULE?

I have already seen a similar question, but that was for always the next weekday.


Solution

  • I was able to create a recurring event based on your requirements using this combined iCAL RRULE

    RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYMONTHDAY=23,24;BYSETPOS=-1
    RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYMONTHDAY=24,25;BYSETPOS=1
    

    RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYMONTHDAY=23,24;BYSETPOS=-1

    • By setting BYSETPOS to -1, this rule will choose the last occurrence within the set occurrences that matched the rule.

    If both 23 and 24 falls on weekdays, it will choose the last occurrence which is the 24th day

    If 24 falls on a Saturday, only one occurrence will be matched based on the rule, Hence it will choose the 23rd day on Friday

    If both 23 and 24 fall on weekends, no event will be created. This is why another rule is needed to resolve this issue.

    RRULE:FREQ=MONTHLY;BYDAY=MO,TU,WE,TH,FR;BYMONTHDAY=24,25;BYSETPOS=1

    • By setting BYSETPOS to 1, this rule will choose the first occurrence within the set occurrences that matched the rule.

    If both 24 and 25 falls on weekdays, it will choose the first occurrence which is the 24th day

    If 24 falls on a Sunday, only one occurrence will be matched based on the rule, Hence it will choose the 25th day on Monday

    If both 24 and 25 fall on weekends, no event will be created based on this rule.