Reputation: 2020
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
});
});
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?
Upvotes: 0
Views: 2123
Reputation: 9381
I found:
0: Sunday
1: Monday
2: Tuesday
3: Wednesday
4: Thursday
5: Friday
6: Saturday
So that part is right. I also see you don't pass date
inside DisableSpecificDates
I'd try (simplified):
$("#datepicker").datepicker({
beforeShowDay: function(date) {
var day = date.getDay();
return [(day != 1 && day != 0 && day != 6)];
}
});
<link href="https://code.jquery.com/ui/1.11.3/themes/smoothness/jquery-ui.css" rel="stylesheet"/>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.11.3/jquery-ui.min.js"></script>
<p>Date:
<input type="text" id="datepicker">
</p>
https://jsfiddle.net/hLa2uuov/2/
Upvotes: 1
Reputation: 16068
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:
weekend[0] || date.getDay() == 1
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
What you want is:
return [(weekend[0] && date.getDay() != 1)];
Which means allow it if it´s a week day different than monday
Upvotes: 2