Jquery UI datepicker. Disable array of Dates

后端 未结 7 711
野的像风
野的像风 2020-11-22 02:21

I have been trying to search for a solution to my Jquery ui datepicker problem and I\'m having no luck. Here\'s what I\'m trying to do...

I have an application where

相关标签:
7条回答
  • 2020-11-22 02:40
    $('input').datepicker({
       beforeShowDay: function(date){
           var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
           return [ array.indexOf(string) == -1 ]
       }
    });
    
    0 讨论(0)
  • beforeShowDate didn't work for me, so I went ahead and developed my own solution:

    $('#embeded_calendar').datepicker({
                   minDate: date,
                    localToday:datePlusOne,
                   changeDate: true,
                   changeMonth: true,
                   changeYear: true,
                   yearRange: "-120:+1",
                   onSelect: function(selectedDateFormatted){
    
                         var selectedDate = $("#embeded_calendar").datepicker('getDate');
    
                        deactivateDates(selectedDate);
                       }
    
               });
    
    
                  var excludedDates = [ "10-20-2017","10-21-2016", "11-21-2016"];
    
                  deactivateDates(new Date());
    
                function deactivateDates(selectedDate){
                    setTimeout(function(){ 
                          var thisMonthExcludedDates = thisMonthDates(selectedDate);
                          thisMonthExcludedDates = getDaysfromDate(thisMonthExcludedDates);
                           var excludedTDs = page.find('td[data-handler="selectDay"]').filter(function(){
                               return $.inArray( $(this).text(), thisMonthExcludedDates) >= 0
                           });
    
                           excludedTDs.unbind('click').addClass('ui-datepicker-unselectable');
                       }, 10);
                }
    
                function thisMonthDates(date){
                  return $.grep( excludedDates, function( n){
                    var dateParts = n.split("-");
                    return dateParts[0] == date.getMonth() + 1  && dateParts[2] == date.getYear() + 1900;
                });
                }
    
                function getDaysfromDate(datesArray){
                      return  $.map( datesArray, function( n){
                        return n.split("-")[1]; 
                    });
                 }
    
    0 讨论(0)
  • 2020-11-22 02:49

    If you want to disable particular date(s) in jquery datepicker then here is the simple demo for you.

    <script type="text/javascript">
        var arrDisabledDates = {};
        arrDisabledDates[new Date("08/28/2017")] = new Date("08/28/2017");
        arrDisabledDates[new Date("12/23/2017")] = new Date("12/23/2017");
        $(".datepicker").datepicker({
            dateFormat: "dd/mm/yy",
            beforeShowDay: function (date) {
                var day = date.getDay(),
                        bDisable = arrDisabledDates[date];
                if (bDisable)
                    return [false, "", ""]
            }
        });
    </script>
    
    0 讨论(0)
  • 2020-11-22 02:55

    You can use beforeShowDay to do this

    The following example disables dates 14 March 2013 thru 16 March 2013

    var array = ["2013-03-14","2013-03-15","2013-03-16"]
    
    $('input').datepicker({
        beforeShowDay: function(date){
            var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
            return [ array.indexOf(string) == -1 ]
        }
    });
    

    Demo: Fiddle

    0 讨论(0)
  • 2020-11-22 02:59

    IE 8 doesn't have indexOf function, so I used jQuery inArray instead.

    $('input').datepicker({
        beforeShowDay: function(date){
            var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
            return [$.inArray(string, array) == -1];
        }
    });
    
    0 讨论(0)
  • 2020-11-22 03:02

    For DD-MM-YY use this code:

    var array = ["03-03-2017', '03-10-2017', '03-25-2017"]

    $('#datepicker').datepicker({
        beforeShowDay: function(date){
        var string = jQuery.datepicker.formatDate('dd-mm-yy', date);
        return [ array.indexOf(string) == -1 ]
        }
    });
    
    function highlightDays(date) {
        for (var i = 0; i < dates.length; i++) {
        if (new Date(dates[i]).toString() == date.toString()) {
            return [true, 'highlight'];
        }
    }
    return [true, ''];
    }
    
    0 讨论(0)
提交回复
热议问题