Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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).

like image 980
arolson101 Avatar asked Mar 02 '16 20:03

arolson101


2 Answers

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.

like image 166
Marten Avatar answered Nov 12 '22 08:11

Marten


The simplest RRULE to get "the last day of the month", regardless of whether it falls on the 28th, 29th, 30th, or 31st would be:

FREQ=MONTHLY;BYMONTHDAY=-1

Your query sounds like that's what you're after.

I don't know if this is supported by the rrule javascript library you mention, however.

like image 12
tgi007 Avatar answered Nov 12 '22 08:11

tgi007