【问题标题】:jQuery Datepicker - Exclude certain days, but allow specific datesjQuery Datepicker - 排除特定日期,但允许特定日期
【发布时间】:2020-12-20 18:06:09
【问题描述】:

我需要在全年的日期选择器中排除周一和周二,但需要在 2020 年 12 月 21 日和 2020 年 12 月 22 日的假期期间允许这样做。

如果不是上述日期之一,我该如何反驳day != 1 && day != 2 并仅使用它?

var excludeDates = ["2020-12-24","2020-12-25","2020-12-26","2020-12-27","2020-12-28","2020-12-29","2020-12-30","2020-12-31","2021-01-01","2021-01-02","2021-01-03",,"2021-01-04","2021-01-05","2021-01-06"];

$( function() {
  $( "#roves_collection_date" ).datepicker({
    minDate : advanceCollection,
    dateFormat: 'dd/mm/yy',
    altFormat: 'yy-mm-dd',
    beforeShowDay: function(date) {
      var day = date.getDay();
      var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
      return [(day != 1 && day != 2 && excludeDates.indexOf(string) == -1];
    }
  });
});

【问题讨论】:

    标签: jquery datepicker


    【解决方案1】:

    如果您将另一个日期数组添加到 include(覆盖任何排除项),那么您可以使用:

    return [((day != 1 && day != 2 && excludeDates.indexOf(string) == -1) 
            || includeDates.indexOf(string) >= 0)];
    

    var excludeDates = ["2020-12-24", "2020-12-25", "2020-12-26", "2020-12-27", "2020-12-28", "2020-12-29", "2020-12-30", "2020-12-31", "2021-01-01", "2021-01-02", "2021-01-03", , "2021-01-04", "2021-01-05", "2021-01-06"];
    var includeDates = ["2020-12-21", "2020-12-22"];
    
    $(function() {
      $("#dte").datepicker({
        //minDate: advanceCollection,
        dateFormat: 'dd/mm/yy',
        altFormat: 'yy-mm-dd',
        beforeShowDay: function(date) {
          var day = date.getDay();
          var string = jQuery.datepicker.formatDate('yy-mm-dd', date);
          console.log(day, string, includeDates.indexOf(string));
          return [((day != 1 && day != 2 && excludeDates.indexOf(string) == -1) 
                  || includeDates.indexOf(string) >= 0)];
        }
      });
    });
    <link href="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.css" rel="stylesheet" />
    
    <script src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.2.4/jquery.js"></script>
    <script src="//cdnjs.cloudflare.com/ajax/libs/jqueryui/1.11.4/jquery-ui.js"></script>
    
    <input type='text' id='dte' />

    【讨论】:

    • 非常感谢您的回答,尽管 excludeDates 数组中的日期确实需要排除,因为商店将关闭。应该包括的是 2020-12-21 和 2020-12-22,因为它们在星期一/星期二,全年都被排除在外,但在这种情况下应该可以选择 - 这可以实现吗?
    • 在澄清要包括哪些日期后更新
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-07-31
    • 1970-01-01
    • 1970-01-01
    • 2012-04-02
    • 2010-10-15
    • 2021-07-01
    相关资源
    最近更新 更多