Skip to content

Commit 93e35d6

Browse files
committed
Added option to disable days of week
1 parent cd46d38 commit 93e35d6

File tree

2 files changed

+37
-1
lines changed

2 files changed

+37
-1
lines changed

README.md

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -128,6 +128,12 @@ Date. Default: End of time
128128

129129
The latest date that may be selected; all later dates will be disabled.
130130

131+
### daysOfWeekDisabled
132+
133+
String, Array. Default: '', []
134+
135+
Days of the week that should be disabled. Values are 0 (Sunday) to 6 (Saturday). Multiple values should be comma-separated. Example: disable weekends: `'0,6'` or `[0,6]`. Can be set in HTML data attribute with `data-date-days-of-week-disabled`.
136+
131137
### autoclose
132138

133139
Boolean. Default: false
@@ -233,6 +239,21 @@ Omit endDate (or provide an otherwise falsey value) to unset the limit.
233239
$('#datepicker').datepicker('setEndDate');
234240
$('#datepicker').datepicker('setEndDate', null);
235241

242+
### setDaysOfWeekDisabled
243+
244+
Arguments:
245+
246+
* daysOfWeekDisabled (String|Array)
247+
248+
Sets the days of week that should be disabled.
249+
250+
$('#datepicker').datepicker('setDaysOfWeekDisabled', [0,6]);
251+
252+
Omit daysOfWeekDisabled (or provide an otherwise falsey value) to unset the disabled days.
253+
254+
$('#datepicker').datepicker('daysOfWeekDisabled');
255+
$('#datepicker').datepicker('daysOfWeekDisabled', null);
256+
236257
## Events
237258

238259
Datepicker class exposes a few events for manipulating the dates.

js/bootstrap-datepicker.js

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,10 @@
113113
this.weekEnd = ((this.weekStart + 6) % 7);
114114
this.startDate = -Infinity;
115115
this.endDate = Infinity;
116+
this.daysOfWeekDisabled = [];
116117
this.setStartDate(options.startDate||this.element.data('date-startdate'));
117118
this.setEndDate(options.endDate||this.element.data('date-enddate'));
119+
this.setDaysOfWeekDisabled(options.daysOfWeekDisabled||this.element.data('date-days-of-week-disabled'));
118120
this.fillDow();
119121
this.fillMonths();
120122
this.update();
@@ -204,6 +206,18 @@
204206
this.updateNavArrows();
205207
},
206208

209+
setDaysOfWeekDisabled: function(daysOfWeekDisabled){
210+
this.daysOfWeekDisabled = daysOfWeekDisabled||[];
211+
if (!$.isArray(this.daysOfWeekDisabled)) {
212+
this.daysOfWeekDisabled = this.daysOfWeekDisabled.split(/,\s*/);
213+
}
214+
this.daysOfWeekDisabled = $.map(this.daysOfWeekDisabled, function (d) {
215+
return parseInt(d, 10);
216+
});
217+
this.update();
218+
this.updateNavArrows();
219+
},
220+
207221
place: function(){
208222
var zIndex = parseInt(this.element.parents().filter(function() {
209223
return $(this).css('z-index') != 'auto';
@@ -296,7 +310,8 @@
296310
if (prevMonth.valueOf() == currentDate) {
297311
clsName += ' active';
298312
}
299-
if (prevMonth.valueOf() < this.startDate || prevMonth.valueOf() > this.endDate) {
313+
if (prevMonth.valueOf() < this.startDate || prevMonth.valueOf() > this.endDate ||
314+
this.daysOfWeekDisabled.indexOf(prevMonth.getUTCDay()) > -1) {
300315
clsName += ' disabled';
301316
}
302317
html.push('<td class="day'+clsName+'">'+prevMonth.getUTCDate() + '</td>');

0 commit comments

Comments
 (0)