How to make past date unselectable in fullcalendar?

前端 未结 15 1228
暗喜
暗喜 2020-12-08 07:46

Problem is, how to disable selectable on PAST DATES in fullcalendar\'s month/week view.

I want to user not allowed to click/select the on past dates.

相关标签:
15条回答
  • 2020-12-08 08:24

    You can use this:

    var start_date= $.fullCalendar.formatDate(start,'YYYY-MM-DD');
    
    var today_date = moment().format('YYYY-MM-DD');
    
    if(check < today)
    {
        alert("Back date event not allowed ");
        $('#calendar').fullCalendar('unselect');
        return false
    }
    
    0 讨论(0)
  • 2020-12-08 08:25

    I have done this in my fullcalendar and it's working perfectly.

    you can add this code in your select function.

     select: function(start, end, allDay) {
        var check = $.fullCalendar.formatDate(start,'yyyy-MM-dd');
        var today = $.fullCalendar.formatDate(new Date(),'yyyy-MM-dd');
        if(check < today)
        {
            // Previous Day. show message if you want otherwise do nothing.
            // So it will be unselectable
        }
        else
        {
            // Its a right date
            // Do something
        }
      },
    

    I hope it will help you.

    0 讨论(0)
  • 2020-12-08 08:25

    I like this approach:

    select: function(start, end) {
        if(start.isBefore(moment())) {
            $('#calendar').fullCalendar('unselect');
            return false;
        }
    }
    

    It will essentially disable selection on times before "now".

    Unselect method

    0 讨论(0)
  • 2020-12-08 08:27

    In Fullcalendar i achieved it by dayClick event. I thought it is the simple way to do it.

    Here is my code..

     dayClick: function (date, cell) {
                  var current_date = moment().format('YYYY-MM-DD')
                  // date.format() returns selected date from fullcalendar
                  if(current_date <= date.format()) {
                    //your code
                  }
                }
    

    Hope it helps past and future dates will be unselectable..

    0 讨论(0)
  • 2020-12-08 08:28

    I have tried this approach, works well.

    $('#calendar').fullCalendar({
       defaultView: 'month',
       selectable: true,
       selectAllow: function(select) {
          return moment().diff(select.start) <= 0
       }
    })
    

    Enjoy!

    0 讨论(0)
  • 2020-12-08 08:28

    In fullcalendar 3.9, you might prefer the validRange function parameter :

    validRange: function(nowDate){
        return {start: nowDate} //to prevent anterior dates
    },
    

    Drawback: this also hides events before that datetime

    0 讨论(0)
提交回复
热议问题