问题
I am trying to use bootstrap datepicker plugin (https://github.com/eternicode/bootstrap-datepicker/blob/release/docs/index.rst) with array of disabled days passed into it.
My code (partial):
var disabled_dates = ["23.03.2014","21.03.2014"];
$("#datepicker").datepicker({
language: "pl",
autoclose: true,
startDate: '+1d',
weekStart: 1,
default: 'dd.mm.yyyy',
beforeShowDay: function(date){
var formattedDate = $.fn.datepicker.DPGlobal.formatDate(date, 'dd.mm.yyyy', 'pl');
if ($.inArray(formattedDate.toString(), disabled_dates) != -1){
return {
enabled : false
};
}
return;
}
});
it works (almost) OK. It disables wrong date. Instead of disabling 23.03.2014 it disabled 24.04.2014. I am not sure where is the issue, on timezone maybe? Any suggestions?
回答1:
Looking inside the plugin, I found out $.fn.datepicker.DPGlobal.formatDate
returns a UTC-formatted string instead of GMT. Try changing the setting of formattedDate variable to this:
var formattedDate = date.toLocaleDateString('pl',{day:'2-digit',year:'numeric',month:'2-digit'});
回答2:
You just need to remove line "startDate: '+1d'," it does not make any sense using it when you have available dates array. Anyway, you can add new Date() function to get atual date.
It will be something like this:
//here you store new Date() Object to get today's date formatted as you want dd.mm.yyy
var disabled_dates = ["23.03.2014","21.03.2014"];
$("#datepicker").datepicker({
language: "pl",
autoclose: true,
//removed line: startDate: '+1d',
weekStart: 1,
default: 'dd.mm.yyyy',
beforeShowDay: function(date){
var formattedDate = $.fn.datepicker.DPGlobal.formatDate(date, 'dd.mm.yyyy', 'pl');
if ($.inArray(formattedDate.toString(), disabled_dates) != -1){
return {
enabled : false
};
}
return;
}
});
来源:https://stackoverflow.com/questions/22566009/bootstrap-datepicker-beforeshowday-and-array-of-disabled-dates