13

Normally this is a event in calendar:

            {

                title: 'Test',

                start: '2012-06-08',

                end: '2012-06-08',

                url: 'test/test'

            },

Works fine. Although how can i make 1 event that shows on every Monday in the calendar? Or every tuesday and so on depend on what weekday/number you enter? Whats the param if there exists one?

If it does not exists, can I somehow modify and add the feature for making this happen? So you can enter a param like repeatEveryWeekDay: 2 (which is monday), then on all future mondays the data will show there.

I tried looking in http://arshaw.com/fullcalendar/docs/event_data/Event_Object/ but can't find anything.

Karem
  • 17,615
  • 72
  • 178
  • 278
  • 1
    Future Readers: There are now better ways to do this (3 years later), but this still is in the top google results. Take a look at http://stackoverflow.com/questions/15161654/recurring-events-in-fullcalendar – DanielST Aug 28 '15 at 15:58

4 Answers4

37

fullCalendar permits that instead of passing an array of events, you can pass a function which, for example, downloads the events from a server, or dynamically generates those events.

Most examples in the documentation use HTTP requests to get events data. But the callback function is still flexible enough to make it work the way you want.

Look at the following example I've just written for you:

$(document).ready(function() {

    var date = new Date();
    var d = date.getDate();
    var m = date.getMonth();
    var y = date.getFullYear();

    $('#calendar').fullCalendar({
        header: {
            left: 'prev,next today',
            center: 'title',
            right: 'month,agendaWeek,agendaDay'
        },
        editable: true,
        events: [
            // some original fullCalendar examples
            {
                title: 'All Day Event',
                start: new Date(y, m, 1)
            },
            {
                title: 'Long Event',
                start: new Date(y, m, d-5),
                end: new Date(y, m, d-2)
            },
            {
                id: 999,
                title: 'Repeating Event',
                start: new Date(y, m, d-3, 16, 0),
                allDay: false
            }
        ]
    });

    // adding a every monday and wednesday events:
    $('#calendar').fullCalendar( 'addEventSource',        
        function(start, end, callback) {
            // When requested, dynamically generate virtual
            // events for every monday and wednesday.
            var events = [];

            for (loop = start.getTime();
                 loop <= end.getTime();
                 loop = loop + (24 * 60 * 60 * 1000)) {

                var test_date = new Date(loop);

                if (test_date.is().monday()) {
                    // we're in Moday, create the event
                    events.push({
                        title: 'I hate mondays - Garfield',
                        start: test_date
                    });
                }

                if (test_date.is().wednesday()) {
                    // we're in Wednesday, create the Wednesday event
                    events.push({
                        title: 'It\'s the middle of the week!',
                        start: test_date
                    });
                }
            } // for loop

            // return events generated
            callback( events );
        }
    );
});

The above function automatically generates an event for every Monday and Wednesday between two dates. The dates are indicated in start and end params. Those params are passed by fullCallendar. Events generated by the above function are returned to fullCallendar through the callback function in third param.

I use DateJS to test if a given date is monday.

UPDATE: If you want to mix static events and/or [more than one] repeatable event, you can use addEventSource.

Leniel Maccaferri
  • 100,159
  • 46
  • 371
  • 480
Christopher Ramírez
  • 1,710
  • 10
  • 13
  • Thanks for your nice answer! Can you show me how I can mix static events with actual dates and more than one repeatable event all together ? – Karem Jun 20 '12 at 09:45
  • 1
    Sure! I've updated the example code. I've used `addEventSource` to add two repeatable events. – Christopher Ramírez Jun 20 '12 at 14:41
  • Thank you so much! Im really close to my goal, I'm mixing php and js to obtain my goal. Now I have a js variable that can be from 1-7, 1 is sunday and saturday 7, how can i do so if it's 2 it should check for is().monday(), and 3 is().tuesday() and so on? :-) Without me having alot of duplicates of the same code to obtain this – Karem Jun 20 '12 at 15:55
  • 2
    Use the `getDay()` [http://www.w3schools.com/jsref/jsref_getday.asp] native function. For example: `if (test_date.getDay() == (repeatable_day - 1))`... where `repeatable_day` it's your variable. Since `getDay()` it's a native function, you won't need DateJS. You can use `switch` if you're going to make more than 2 comparisons. – Christopher Ramírez Jun 20 '12 at 16:05
  • Thanks so much. Wonderful!!! Just one very very last thing, if i have a variable stopDate with 2012-06-30, can i in anyway make so it stop checks/applies on Mondays, after that date? Cant i do like if(test_date == '2012-06-30') { break; } although i need test_date in yyyy-mm-dd , can you show me how ? – Karem Jun 20 '12 at 16:55
  • `2012-06-30` is a date in ISO format. Firsr declare your stop date: `var stop_date = new Date('2012-06-30 8:00')`. On the loop comparison replace `loop <= end.getTime()` for `(loop <= end.getTime()) && (loop <= stop_date)`. – Christopher Ramírez Jun 20 '12 at 18:22
  • @ChristopherRamírez Hi, why is that I get an error in my end `Uncaught TypeError: start.getTime is not a function`what I'm missing? – leonardeveloper Mar 27 '16 at 08:20
8

A little update to Christopher Ramírez's post. Using fullCalendar at v2.2.3 and momentjs at v2.8.4 you need to amend couple of lines in the snippet above to make it work:

First:

function(start, end, callback) {}

to

function(start, end, status, callback) {}

as the third parameter is a boolean and the fourth is the callback function

Then:

for (loop = start.getTime();
     loop <= end.getTime();
...

to

for (loop = start._d.getTime();
     loop <= end._d.getTime();

as the start and end objects passed to the function are not Date objects and calling .getTime() on them will end up in 'Undefined is not a function' error.

And thats it! This amendments made it work for me.

Belgor
  • 181
  • 1
  • 2
  • 9
1

It's the ID field of the event object that determines the repeatability of that event. Use the same ID for all your repeating events for a particular day of the week.

ganeshk
  • 5,583
  • 3
  • 27
  • 31
  • Hmm I dont get it? E.g the example above i want it to repeat every monday, so instead of having TONS of elements in the events[] with monday dates of the year, Im looking for One element that the fullcalendar shows every future monday without a specific date given ? – Karem Jun 17 '12 at 16:10
  • 1
    I don't think FullCalendar by default supports that. If there is no data in the events[] object, then how should it display those events? You can set this programmatically using a callback like 'viewDisplay', but even there you will need to create event objects. Short of editing the code to add a new configuration parameter, I think you will need to achieve this by having one object per event in your events[] array - with the same ID for repeating events. – ganeshk Jun 17 '12 at 16:22
  • Why should it have same ID whats the matter here then ? And would i need to generate by PHP all the upcoming monday date's of the current year just to display them each monday ? ? – Karem Jun 17 '12 at 16:37
1

The best way I found to solve a repeat event is to setup a JSON call to your backend language (i'm using Django/Python).

so in the calendar options i would have:

events: { url: '/event-data', cache: false },

Then in my backend I take advantage of the parameters that get sent for the current displayed calendar 'start' and 'end' URL GET variables. I.e.:

hxxp://example.com:8000/event-data?start=2014-09-28&end=2014-11-09&_=1413553318353

a Get request would be sent from the events options, I then build events from a database/source file that lists repeat events along with regular calendar events, and send back just the list of events occurring in that date range in a JSON list.

the
  • 21,007
  • 11
  • 68
  • 101
warath-coder
  • 2,087
  • 1
  • 17
  • 21