简体   繁体   中英

Jquery UI DatePicker Restrict Set Of Date

I am working on Jquery-UI Datepicker for hotel domain project. Basically hotel have some of Packages/Offers that are not valid on some of date durations. This durations are coming from database. Between these date user can't select date for booking from Jquery-UI Calendar. I don't know how to implement this.

$(".calendar").datepicker({
    dateFormat: 'dd/mm/yy',
    minDate:0
});
/* an array of days which are not applicable for packages/offers Booking */
var disabledDays = ["10-25-2015","10-26-2015","10-27-2015","10-28-2015","11-3-2015","11-4-2015","11-5-2015","11-20-2015","11-21-2015","11-22-2015","12-12-2015","12-13-2015"];

/* utility functions */
function getBookedDays(date) {
    var m = date.getMonth();
    var d = date.getDate();
    var y = date.getFullYear();
    //
}

http://jsfiddle.net/ANJYR/34yfb2zs/1/

When calendar open user can't select date from 25 Oct 2015 To 28 Oct 2015 as so on dates.

You can try like this:

$('.calendar').datepicker({
    beforeShowDay: function(date){
        var str = jQuery.datepicker.formatDate('yy-mm-dd', date);
        return [ disabledDays.indexOf(str) == -1 ]
    }
});

JSFIDDLE DEMO

See demo example at: jsFiddle Example

var unavailableDates = ["28-10-2015", "29-10-2015", "27-10-2015"];

function unavailable(date) {
dmy = date.getDate() + "-" + (date.getMonth() + 1) + "-" + date.getFullYear();
if ($.inArray(dmy, unavailableDates) == -1) {
    return [true, ""];
} else {
    return [false, "", "Unavailable"];
}
}

$(function() {
$("#iDate").datepicker({
    defaultDate: new Date("1-10-2015"),
    dateFormat: 'dd MM yy',
    beforeShowDay: unavailable
});

});
$(".calendar").datepicker({
    dateFormat: 'dd/mm/yy',
    minDate:0,
    beforeShowDay: getBookedDays
});
/* an array of days which are not applicable for packages/offers Booking */
var disabledDays = ["10-25-2015","10-26-2015","10-27-2015","10-28-2015","11-3-2015","11-4-2015","11-5-2015","11-20-2015","11-21-2015","11-22-2015","12-12-2015","12-13-2015"];

/* utility functions */


function getBookedDays(date) {
    var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
    for (i = 0; i < disabledDays.length; i++) {
        if($.inArray((m+1) + '-' + d + '-' + y,disabledDays) != -1) {
            return [false];
        }
    }
    return [true];
}

DEMO

Try this:

var disableddates = ["28-10-2015", "29-10-2015", "27-10-2015"];

function DisableSpecificDates(date) {

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

 var currentdate = (m + 1) + '-' + d + '-' + y ;

 for (var i = 0; i < disableddates.length; i++) {

 if ($.inArray(currentdate, disableddates) != -1 ) {
 return [false];
 }
 }

}

 $(function() {
  $( ".calendar" ).datepicker({
  beforeShowDay: DisableSpecificDates
  });
 });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM