Search code examples
recurring-eventsrrulefullcalendar-4

FullCalendar V4 - How to account for shorter months in a recurring event series?


I'm using FullCalendar v4-alpha-3 with the RRule plugin to generate recurring events. It works as expected with only one problem: how do I modify a recurring event to account for months with fewer days than the starting month in a series?

For example, if the first monthly occurrence happens on January 29, 2019; the event will be repeated on the 29th of all subsequent months except in February since it only has 28 days (leap years excluded).

I've tried resetting dtstart to the first day of the following month. It works, except the event is no longer recursive.

Here's a stripped down snippet of my setup:

let calendar = new Calendar(calendarEl, {
  plugins: [ rrulePlugin ],
  events: [
    {
      rrule: 'DTSTART:20190129 RRULE:FREQ=MONTHLY;UNTIL=20200130;COUNT=13;BYMONTHDAY=29'
    }
  ],
  eventRender: function(info) {
    ...

    // reset start date to the first day of the following month 
    // if current month has fewer days than base month

    let start = event.start;
    let day = start.getDate();            

    let now = new Date();
    let currentMonth = now.getMonth();     
    let currentYear = now.getFullYear();
    let daysInCurrent = getDaysInMonth(currentMonth + 1, currentYear);

    let nextStart = start;
    if (day > daysInCurrent) {
        nextStart = new Date(currentYear, currentMonth + 1, 1);    
        event.setStart(nextStart);   
        event.setEnd(null);                     
    } 
  }
});

I'd appreciate any insight.


Solution

  • Not quite the solution I hoped for, but RRule's bysetpos property seems to offer the next best alternative as it allows for a fallback date in case the one specified doesn't exist.

    For example, the following would generate an occurrence on the 30th of every month; or the last day of the month if the 30th doesn’t exist:

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

    Sourced from: https://icalevents.com/2555-paydays-last-working-days-and-why-bysetpos-is-useful/