jQuery Datepicker - Disable weekends / holidays and the next three working days combined

前端 未结 3 1632
花落未央
花落未央 2020-12-28 11:24

Using this rather neat approach I can disable weekends and holidays from the datepicker.

However, I want to combine this with the disabling of the next three busines

3条回答
  •  囚心锁ツ
    2020-12-28 12:03

    My case was very close, but idea is that number of business days should be added to script. I replaced AddWeekDays() to AddBusinessDays() from Junto example. So when calculation minday it skip all non-business days.

        //holidays
        var natDays = [
          [1, 1, 'uk'],
          [12, 25, 'uk'],
          [12, 26, 'uk']
        ];
    
        var dateMin = new Date();
        var weekDays = AddBusinessDays(3);
    
        dateMin.setDate(dateMin.getDate() + weekDays);
    
        function AddBusinessDays(weekDaysToAdd) {
          var curdate = new Date();
          var realDaysToAdd = 0;
          while (weekDaysToAdd > 0){
            curdate.setDate(curdate.getDate()+1);
            realDaysToAdd++;
            //check if current day is business day
            if (noWeekendsOrHolidays(curdate)[0]) {
              weekDaysToAdd--;
            }
          }
          return realDaysToAdd;
    
        }
    
        function noWeekendsOrHolidays(date) {
            var noWeekend = $.datepicker.noWeekends(date);
            if (noWeekend[0]) {
                return nationalDays(date);
            } else {
                return noWeekend;
            }
        }
        function nationalDays(date) {
            for (i = 0; i < natDays.length; i++) {
                if (date.getMonth() == natDays[i][0] - 1 && date.getDate() == natDays[i][1]) {
                    return [false, natDays[i][2] + '_day'];
                }
            }
            return [true, ''];
        }
    
    
        $('#datepicker').datepicker(
        {
            inline: true,
            beforeShowDay: noWeekendsOrHolidays,
            altField: '#txtCollectionDate',
            showOn: "both",
            dateFormat: "dd/mm/yy",
            firstDay: 1,
            changeFirstDay: false,
            minDate: dateMin
        });
    

提交回复
热议问题