Search code examples
javascriptdatefullcalendarfullcalendar-5

Disable past date in FullCalendar for dateClick


Those who still looking for a solution on how to disable past date click on Dateclick event on FullCalendar. can try see solution below, if any of the reader had other idea can post other solution below. It would be much better if there is another easy way. The more solution, the better it would be.


Solution

  • It's possible to improve on your string-manipulation of dates, by doing a simple comparison as follows:

    dateClick: function (info) {
      var clickedDate = getDateWithoutTime(info.date);
      var nowDate = getDateWithoutTime(new Date())
      if (clickedDate >= nowDate) alert("Future date");
      else alert("Past date");
    }
    

    It is helped by this function:

    //get date without the time of day
    function getDateWithoutTime(dt)
    {
      dt.setHours(0,0,0,0);
      return dt;
    }
    

    Demo: https://codepen.io/ADyson82/pen/ZErwJGx


    One other option to achieve the same goal is to use the validRange functionality. This lets you dynamically set a start date, before which all dates are disabled:

    validRange: function (nowDate) {
      return {
        start: nowDate
      };
    },
    

    Demo: https://codepen.io/ADyson82/pen/VwQgWJO

    The slight downside of this though is that it also hides any events which occur before the current date - you may not want this.


    Therefore, another alternative is to use selectAllow to restrict where the user can select. Note that this relies on you using the select callback rather than dateClick - but that should not be a big problem.

    selectAllow: function (info) {
      return (info.start >= getDateWithoutTime(new Date()));
    }
    

    Demo: https://codepen.io/ADyson82/pen/VwQgzZJ