twitter-bootstrap 引导日期选择器、beforeShowDay 和禁用日期数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22566009/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 20:20:32  来源:igfitidea点击:

bootstrap datepicker, beforeShowDay and array of disabled dates

twitter-bootstrapdatepickerbootstrap-datepicker

提问by dease

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.

我正在尝试使用 bootstrap datepicker 插件(https://github.com/eternicode/bootstrap-datepicker/blob/release/docs/index.rst),其中包含禁用天数的数组。

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?

它工作(几乎)可以。它禁用错误的日期。它没有禁用 23.03.2014,而是禁用了 24.04.2014。我不确定问题出在哪里,可能是在时区?有什么建议?

采纳答案by Justin Paul Pa?o

Looking inside the plugin, I found out $.fn.datepicker.DPGlobal.formatDatereturns a UTC-formatted string instead of GMT. Try changing the setting of formattedDatevariable to this:

查看插件内部,我发现$.fn.datepicker.DPGlobal.formatDate返回一个 UTC 格式的字符串而不是 GMT。尝试将formattedDate变量的设置更改为:

var formattedDate = date.toLocaleDateString('pl',{day:'2-digit',year:'numeric',month:'2-digit'});

回答by meetnick

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.

您只需要删除“startDate:'+1d'”行,当您有可用的日期数组时,使用它没有任何意义。无论如何,您可以添加 new 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;
      }
  });