Search code examples
javascriptrrule

rrule for repeating monthly on the 31st or closest day


How would you specify a rrule for an event on the 31st day of the month (or 30th, or 29th) that recurs every month, where if the month doesn't have enough days it picks the closest (i.e. for February it would pick the 28th or 29th, for April it would pick the 30th)?

Technically I'm using the rrule javascript library if that's relevant.

To add context, I have a form where the user can specify a start date an recurrence (yearly, monthly, weekly, daily), like for a bill. If a bill is usually due on the 30th then in February it will be due the 28th (or 29th).


Solution

  • There is a new extension to RRULE called RSCALE to cover that case. Unfortunately it's not widely supported yet. Not sure about the Javascript rrule library you're using, but you should open an issue if it's not the case.

    Using the RSCALE extension your RRULE would look like so:

    FREQ=MONTHLY;RSCALE=GREGORIAN;BYMONTHDAY=31;SKIP=BACKWARD
    

    Events having this RRULE recur on every 31st each month, unless that day doesn't exist in which case SKIP=BACKWARD says "use the previous valid day".

    Edit

    I've just been made aware of another way to express this without RSCALE:

    31st each month with a fallback to the last valid day in that month:

    FREQ=MONTHLY;BYMONTHDAY=28,29,30,31;BYSETPOS=-1
    

    30th each month with a fall back to the 28th or 29th (in leap years) in February

    FREQ=MONTHLY;BYMONTHDAY=28,29,30;BYSETPOS=-1
    

    29th each month with a fall back to the 28th in February in non-leap years

    FREQ=MONTHLY;BYMONTHDAY=28,29;BYSETPOS=-1
    

    However, as one can see this is clearly more intuitive with RSCALE.