0

I need to exclude Monday and Tuesday from a datepicker throughout the year, but need to allow this during the holiday season for 2020-12-21 and 2020-12-22.

How could I counter the day != 1 && day != 2 and only use this if not one of the above dates?

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];
    }
  });
});
0

1 Answer 1

1

If you add another array for dates to include (overwrite any excludes) then you can use:

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' />

Sign up to request clarification or add additional context in comments.

2 Comments

Many thanks for the answer, although dates within the excludeDates array do need to be excluded as the store will be shut. The ones that should be included are the 2020-12-21 and 2020-12-22 as these fall on Monday/Tuesday, which are excluded throughout the year, but should be selectable in this case - is this achievable?
Updated following clarification of which dates are to be included

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.