是否可以让jQuery UI DatePicker禁用周六和周日(以及节假日)?

我使用DatePicker来选择约会日期。我已经将日期范围设置为仅下个月。那很好用。我想从可用选项中排除星期六和星期日。这能做到吗?如果有,怎么做?

157013 次浏览

有一个beforeShowDay选项,它接受为每个日期调用的函数,如果日期是允许的,则返回true,如果不是,则返回false.从文档中:


在Showday之前

此函数接受日期作为参数且必须返回[0]等于TRUE/FALSE的数组,指示此日期是否可选,以及1等于CSS类名或' '表示默认显示。在DatePicker中为每一天调用它,然后才显示它。

在DatePicker中显示一些国定假日。

$(".selector").datepicker({ beforeShowDay: nationalDays})


natDays = [
[1, 26, 'au'], [2, 6, 'nz'], [3, 17, 'ie'],
[4, 27, 'za'], [5, 25, 'ar'], [6, 6, 'se'],
[7, 4, 'us'], [8, 17, 'id'], [9, 7, 'br'],
[10, 1, 'cn'], [11, 22, 'lb'], [12, 12, 'ke']
];


function nationalDays(date) {
for (i = 0; i < natDays.length; i++) {
if (date.getMonth() == natDays[i][0] - 1
&& date.getDate() == natDays[i][1]) {
return [false, natDays[i][2] + '_day'];
}
}
return [true, ''];
}

有一个名为NoWeekends的内置函数,可以防止选择周末。

$(".selector").datepicker({ beforeShowDay: $.datepicker.noWeekends })

要将两者结合起来,您可以执行以下操作(假设nationalDays是上面的函数):

$(".selector").datepicker({ beforeShowDay: noWeekendsOrHolidays})


function noWeekendsOrHolidays(date) {
var noWeekend = $.datepicker.noWeekends(date);
if (noWeekend[0]) {
return nationalDays(date);
} else {
return noWeekend;
}
}

更新:请注意,从jQuery UI 1.8.19开始,beforeShowDay选项还接受可选的第三个参数,即弹出工具提示

如果您根本不想让周末出现,只需:

CSS

th.ui-datepicker-week-end,
td.ui-datepicker-week-end {
display: none;
}

在此版本中,月、日、年确定要在日历上锁定的日期。

$(document).ready(function (){
var d         = new Date();
var natDays   = [[1,1,2009],[1,1,2010],[12,31,2010],[1,19,2009]];


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


for (i = 0; i < natDays.length; i++) {
if ((m == natDays[i][0] - 1) && (d == natDays[i][1]) && (y == natDays[i][2]))
{
return [false];
}
}
return [true];
}
function noWeekendsOrHolidays(date) {
var noWeekend = $.datepicker.noWeekends(date);
if (noWeekend[0]) {
return nationalDays(date);
} else {
return noWeekend;
}
}
$(function() {
$(".datepicker").datepicker({


minDate: new Date(d.getFullYear(), 1 - 1, 1),
maxDate: new Date(d.getFullYear()+1, 11, 31),


hideIfNoPrevNext: true,
beforeShowDay: noWeekendsOrHolidays,
});
});
});

此版本的代码将使您从SQL数据库中获取假日日期,并在UI DatePicker中禁用指定的日期


$(document).ready(function (){
var holiDays = (function () {
var val = null;
$.ajax({
'async': false,
'global': false,
'url': 'getdate.php',
'success': function (data) {
val = data;
}
});
return val;
})();
var natDays = holiDays.split('');


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


for (var i = 0; i ‘ natDays.length-1; i++) {
var myDate = new Date(natDays[i]);
if ((m == (myDate.getMonth())) && (d == (myDate.getDate())) && (y == (myDate.getFullYear())))
{
return [false];
}
}
return [true];
}


function noWeekendsOrHolidays(date) {
var noWeekend = $.datepicker.noWeekends(date);
if (noWeekend[0]) {
return nationalDays(date);
} else {
return noWeekend;
}
}
$(function() {
$("#shipdate").datepicker({
minDate: 0,
dateFormat: 'DD, d MM, yy',
beforeShowDay: noWeekendsOrHolidays,
showOn: 'button',
buttonImage: 'images/calendar.gif',
buttonImageOnly: true
});
});
});
在SQL中

创建一个数据库,并将假期日期以mm/DD/yyyy格式作为VARCHAR 将以下内容放在文件getdate.PHP中


[php]
$sql="SELECT dates FROM holidaydates";
$result = mysql_query($sql);
$chkdate = $_POST['chkdate'];
$str='';
while($row = mysql_fetch_array($result))
{
$str .=$row[0].'';
}
echo $str;
[/php]

快乐编码!:-)

这些答案很有帮助。谢谢。

我下面的贡献添加了一个数组,其中多天可以返回false(我们每周二、周三和周四关闭)。我把特定日期加上年份和无周末功能捆绑在一起。

如果您希望周末休息,请将[Saturday]、[Sunday]添加到ClosedDays数组。

$(document).ready(function(){


$("#datepicker").datepicker({
beforeShowDay: nonWorkingDates,
numberOfMonths: 1,
minDate: '05/01/09',
maxDate: '+2M',
firstDay: 1
});


function nonWorkingDates(date){
var day = date.getDay(), Sunday = 0, Monday = 1, Tuesday = 2, Wednesday = 3, Thursday = 4, Friday = 5, Saturday = 6;
var closedDates = [[7, 29, 2009], [8, 25, 2010]];
var closedDays = [[Monday], [Tuesday]];
for (var i = 0; i < closedDays.length; i++) {
if (day == closedDays[i][0]) {
return [false];
}


}


for (i = 0; i < closedDates.length; i++) {
if (date.getMonth() == closedDates[i][0] - 1 &&
date.getDate() == closedDates[i][1] &&
date.getFullYear() == closedDates[i][2]) {
return [false];
}
}


return [true];
}








});
$("#selector").datepicker({ beforeShowDay: highlightDays });


...


var dates = [new Date("1/1/2011"), new Date("1/2/2011")];


function highlightDays(date) {


for (var i = 0; i < dates.length; i++) {
if (date - dates[i] == 0) {
return [true,'', 'TOOLTIP'];
}
}
return [false];


}

这里大家喜欢的解决方案似乎很激烈……我个人认为做这样的事情要容易得多:

       var holidays = ["12/24/2012", "12/25/2012", "1/1/2013",
"5/27/2013", "7/4/2013", "9/2/2013", "11/28/2013",
"11/29/2013", "12/24/2013", "12/25/2013"];


$( "#requestShipDate" ).datepicker({
beforeShowDay: function(date){
show = true;
if(date.getDay() == 0 || date.getDay() == 6){show = false;}//No Weekends
for (var i = 0; i < holidays.length; i++) {
if (new Date(holidays[i]).toString() == date.toString()) {show = false;}//No Holidays
}
var display = [show,'',(show)?'':'No Weekends or Holidays'];//With Fancy hover tooltip!
return display;
}
});

这样你的日期是人类可读的。这并没有什么不同,只是这样对我来说更有意义。

DatePicker内置了此功能!

$( "#datepicker" ).datepicker({
beforeShowDay: $.datepicker.noWeekends
});

http://api.jqueryui.com/datepicker/#utility-noweekends.

您可以使用NoWeekends函数来禁用周末选择

  $(function() {
$( "#datepicker" ).datepicker({
beforeShowDay: $.datepicker.noWeekends
});
});

在最新版本的Bootstrap 3(Bootstrap-DatePicker.JS)中,beforeShowDay需要以下格式的结果:

{ enabled: false, classes: "class-name", tooltip: "Holiday!" }

或者,如果您不关心CSS和工具提示,则只需返回一个布尔false,使日期不可选。

此外,_0中没有_ABC,因此您需要执行以下操作:

var HOLIDAYS = {  // Ontario, Canada holidays
2017: {
1: { 1: "New Year's Day"},
2: { 20: "Family Day" },
4: { 17: "Easter Monday" },
5: { 22: "Victoria Day" },
7: { 1: "Canada Day" },
8: { 7: "Civic Holiday" },
9: { 4: "Labour Day" },
10: { 9: "Thanksgiving" },
12: { 25: "Christmas", 26: "Boxing Day"}
}
};


function filterNonWorkingDays(date) {
// Is it a weekend?
if ([ 0, 6 ].indexOf(date.getDay()) >= 0)
return { enabled: false, classes: "weekend" };
// Is it a holiday?
var h = HOLIDAYS;
$.each(
[ date.getYear() + 1900, date.getMonth() + 1, date.getDate() ],
function (i, x) {
h = h[x];
if (typeof h === "undefined")
return false;
}
);
if (h)
return { enabled: false, classes: "holiday", tooltip: h };
// It's a regular work day.
return { enabled: true };
}


$("#datePicker").datepicker({ beforeShowDay: filterNonWorkingDays });

周六和周日你可以这样做。

             $('#orderdate').datepicker({
daysOfWeekDisabled: [0,6]
});

为了禁用周末,API有一个内置功能。

$('#data_1 .input-group.date').datepicker({
daysOfWeekDisabled: [0,6],
});
  

0=星期日

6=星期日