Search code examples
javascriptfullcalendarrecurring-events

Fullcalendar Repeating Events


I am using fullcalendar, and want to repeat an event once in month (on monday) form August to November. I have been able to repeat the event , but the event repeats four times in a month on each monday of the week -while I need it to repeat once on the first monday after the start date . Below date ranges were passed along with the DOW (days of week ) paramater.

 var repeatingEvents = [{
title:"My repeating event",
id: 1,
start: '10:00', 
end: '14:00', 
dow: [ 1, 4 ], 
ranges: [{ //repeating events are only displayed if they are within at least one of the following ranges.
    start: moment().startOf('week'), //next two weeks
    end: moment().endOf('week').add(7,'d'),
},{
    start: moment('2015-02-01','YYYY-MM-DD'), //all of february
    end: moment('2015-02-01','YYYY-MM-DD').endOf('month'),
},/*...other ranges*/],
},/*...other repeating events*/];

Is there any way I can repeat it once in a month , running it form start date to end date? Any Assistance would be appreciated


Solution

  • If you can't generate your events on the server side as @ADyson suggested, you could do it in Javascript. This finds the first Monday of the month, between the specified start and end dates.

    var id=0, event, events = [],
        start=moment('2017-08-01'),
        end=moment('2017-10-31');
    
    while (start.isBefore(end)) {
        id++;
        if (start.day() === 'Monday') {
            day = start.format('YYYY-MM-DD');
        } else {
            day = start.add(1, 'weeks').startOf('isoWeek').format('YYYY-MM-DD');
        }
        event = {
            title:"My repeating event",
            id: id,
            start: day + ' 10:00:00', 
            end: day + ' 14:00:00', 
        }
        events.push(event);
        start.add(1, 'month').startOf('month');
    }
    

    And then use your constructed array of events in your calendar:

    $('#calendar').fullCalendar({
        events: events,
        // ...