Skip to content

Instantly share code, notes, and snippets.

@WebSofter
Last active October 3, 2017 06:39
Show Gist options
  • Save WebSofter/4b1a3f264a61c9867df3846be16f0ecd to your computer and use it in GitHub Desktop.
Save WebSofter/4b1a3f264a61c9867df3846be16f0ecd to your computer and use it in GitHub Desktop.
jQueryUI:DatePicker:Ограничение выборки определенных дней #jQuery #DatePicker
/* *****************************Запретить определенные дни******************************* */
var disabledDays = ["1-10-2017","5-10-2017","10-10-2017","11-10-2017","15-10-2017","7-10-2017"];
/* utility functions */
function nationalDays(date) {
var m = date.getMonth(), d = date.getDate(), y = date.getFullYear();
//console.log('Checking (raw): ' + m + '-' + d + '-' + y);
for (i = 0; i < disabledDays.length; i++) {
if($.inArray((m+1) + '-' + d + '-' + y,disabledDays) != -1 || new Date() > date) {
//console.log('bad: ' + (m+1) + '-' + d + '-' + y + ' / ' + disabledDays[i]);
return [false];
}
}
//console.log('good: ' + (m+1) + '-' + d + '-' + y);
return [true];
}
function noWeekendsOrHolidays(date) {
var noWeekend = jQuery.datepicker.noWeekends(date);
return noWeekend[0] ? nationalDays(date) : noWeekend;
}
//
$("#datepicker").datepicker({showOn:'focus', "dateFormat":"dd-mm-yy", constrainInput: true,
beforeShowDay: noWeekendsOrHolidays}).focus();
/* *********************************Запретить все, кроме указанных************************************** */
var datelist = []; // initialize empty array
var result = "28-10-2017,29-10-2017,01-10-2017,09-10-2017,03-10-2017"; // dummy result
datelist = result.split(","); // populate the array
var beforeFunc = function(d) {
// normalize the date for searching in array
var dmy = "";
dmy += ("00" + d.getDate()).slice(-2) + "-";
dmy += ("00" + (d.getMonth() + 1)).slice(-2) + "-";
dmy += d.getFullYear();
return [$.inArray(dmy, datelist) >= 0 ? true : false, ""];
}
$("#datepicker").datepicker({showOn:'focus', "dateFormat":"dd-mm-yy", constrainInput: true,
beforeShowDay: beforeFunc}).focus();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment