javascriptfullcalendarfullcalendar-5

JS - FullCalendar : Listen when the month changes


I'm having a little issue with FullCalendar v5, I configured it with the dayGridMonth View and I would like to listen when the user changes the current month ...

For example, if he is seeing February and click on next he'll see march, so I was expected for a handler like onChange or onMonthChange but I didn't find anything in the documentation to do something like this ...

I figured how to get around the problem by making my own prev / next buttons and triggering my custom handler on the click ... But I would like to know if there is a vanilla way to do it?

Thanks for your answers.


Solution

  • As mentionned by @Devsi Odedra, the answer was datesSet

    The doc : https://fullcalendar.io/docs/datesSet

    This is my actual code if it can help someone :

    new Calendar(document.getElementById("calendar"), {
        plugins: [ dayGridPlugin, interactionPlugin ],
        datesSet: event => {
            // As the calendar starts from prev month and end in next month I take the day between the range
            var midDate = new Date((event.start.getTime() + event.end.getTime()) / 2).getMonth()
            var month = `0${ midDate.getMonth() + 1 }`.splice(0, -1)
            doSomethingOnThisMonth(month, midDate.getFullYear())
        }
    });
    
    function doSomethingOnThisMonth(month, year) {
        fetch(`myApi.com/something?month=${ month }&year=${ year }`)
            .then((result) => {
                // Do something
            })
    }