bootstrap datepicker, beforeShowDay and array of d

2019-04-08 13:57发布

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?

2条回答
家丑人穷心不美
2楼-- · 2019-04-08 14:30

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'});
查看更多
一纸荒年 Trace。
3楼-- · 2019-04-08 14:38

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;
      }
  });
查看更多
登录 后发表回答