问题描述
我正在尝试排除周末,从我的datepicker开始每周的日期和星期一。这是我到目前为止:
I'm trying to exclude weekends, an array of dates and monday of every week from my datepicker. This is what I have so far:
var disableddates = ["12-21-2015", "12-24-2015", "1-4-2016", "1-5-2016"];
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];
}
}
var weekend = $.datepicker.noWeekends(date);
return [(weekend[0] || date.getDay() == 1)]; // I'm trying to disable Monday here
}
$(function() {
$("#date").datepicker( {
minDate: setMinDate,
maxDate: "+2M",
beforeShowDay: DisableSpecificDates
});
});
具体日期(存储在 disableddates
数组)与周末一样排除在外,但星期一仍然可以选择 - 有谁知道我在哪里出错,还是其他解决方案?
The specific dates (stored in the disableddates
array) are excluded, as are the weekends, but Monday is still selectable - does any one know where i'm going wrong, or an alternative solution to this?
推荐答案
所以如果我明白了,你只要星期二到星期五的日期。检查什么 $。datepicker.noWeekends(date);
,如果它是一个工作日,它将返回1,如果是周末日期,则返回0。所以让我们检查你的代码:
So if I understood right you want just tuesday-friday dates. Checked what $.datepicker.noWeekends(date);
does and it will return a 1 if it´s a weekday and 0 if it´s a weekend date. So let's check your code:
周末[0] || date.getDay()== 1
如果周末[0]
是false ,这意味着 date.getDay()== 0
或 == 6
(星期日或星期六)。但是对于 date.getDay()== 1
, weekend [0]
将永远是正确的。所以你总是允许星期一,因为 true || true == true
If weekend[0]
is false, that means that date.getDay() == 0
or == 6
(sunday or saturday). But weekend[0]
will always be true for date.getDay() == 1
. So you are always allowing monday, as true || true == true
你想要的是:
return [(weekend[0] && date.getDay() != 1)];
这意味着如果星期一星期不同于星期一
Which means allow it if it´s a week day different than monday
这篇关于Javascript datepicker排除周末,一系列日期&每周的特定日子的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!