bootstrap datepicker, beforeShowDay and array of disabled dates

北战南征 提交于 2019-12-21 16:59:02

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!