jQuery Datepicker - 禁用周末/节假日和接下来的三个工作日

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2029626/
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-08-26 12:37:44  来源:igfitidea点击:

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

jquerydatepicker

提问by Rebecca

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

使用这种相当简洁的方法,我可以从日期选择器中禁用周末和假期。

However, I want to combine this with the disabling of the next three business days from today's date. Simply setting the minimum date is relatively easy:

但是,我想将此与从今天起禁用接下来的三个工作日结合起来。简单地设置最小日期相对容易:

var dateMin = new Date();
dateMin.setDate(dateMin.getDate() + 3);
$(function() {

 $('#txtCollectionDate').datepicker(
 {
  beforeShowDay: noWeekendsOrHolidays,
  showOn: "both",
  dateFormat: "dd/mm/yy",
  firstDay: 1,
  changeFirstDay: false,
  minDate: dateMin
 });

});

However, what I really need to a function that calculates the business days:

但是,我真正需要一个计算工作日的函数:

var dateMin = new Date();
dateMin.setDate(AddBusinessDays(3));

Anyone able to convert thisto JavaScript?

任何人都能够转换为JavaScript?

回答by Rebecca

Found the solution here.

这里找到了解决方案。

Code (apologies for the ASP):

代码(为 ASP 道歉):

<html>
    <head>
        <title>Collection Date</title>

        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script> 
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js"></script> 
        <link type="text/css" rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/themes/base/jquery-ui.css" /> 

    </head>
<body>

<h1>jQuery Datepicker Test</h1>

<%
If Request.Form("Submit") <> "" then
%>
<h2>Form Post Confirmation</h2>
<table border="1">
<tr>
    <td><B>Form Variable</B></td>
    <td><B>Value</B></td>
</tr>
<%
Dim Item
For Each Item In Request.Form
%>
<tr>
    <td><%=Item %></td>
    <td><%=Request.Form(Item) %></td>
</tr>
<% Next %>
</table>
<%
End If
%>

<form name="Form1" method="post" action="TestDatePicker.asp" id="Form1">

<h2>Collection Form</h2>

<fieldset>
    <legend>Choose the collection date</legend>

    <div id="datepicker"></div>
    <input type="text" id="txtCollectionDate" name="txtCollectionDate" class="requiredField" style="display: none;" />

</fieldset>

<input type="submit" name="submit" value="Submit" />

</form>

<script type="text/javascript">
    $(document).ready(function() {

        var dateMin = new Date();
        var weekDays = AddWeekDays(3);

        dateMin.setDate(dateMin.getDate() + weekDays);

        var natDays = [
          [1, 1, 'uk'],
          [12, 25, 'uk'],
          [12, 26, 'uk']
        ];

        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, ''];
        }
        function AddWeekDays(weekDaysToAdd) {
            var daysToAdd = 0
            var mydate = new Date()
            var day = mydate.getDay()
            weekDaysToAdd = weekDaysToAdd - (5 - day)
            if ((5 - day) < weekDaysToAdd || weekDaysToAdd == 1) {
                daysToAdd = (5 - day) + 2 + daysToAdd
            } else { // (5-day) >= weekDaysToAdd
                daysToAdd = (5 - day) + daysToAdd
            }
            while (weekDaysToAdd != 0) {
                var week = weekDaysToAdd - 5
                if (week > 0) {
                    daysToAdd = 7 + daysToAdd
                    weekDaysToAdd = weekDaysToAdd - 5
                } else { // week < 0
                    daysToAdd = (5 + week) + daysToAdd
                    weekDaysToAdd = weekDaysToAdd - (5 + week)
                }
            }

            return daysToAdd;
        }

        $('#datepicker').datepicker(
        {
            inline: true,
            beforeShowDay: noWeekendsOrHolidays,
            altField: '#txtCollectionDate',
            showOn: "both",
            dateFormat: "dd/mm/yy",
            firstDay: 1,
            changeFirstDay: false,
            minDate: dateMin
        });
    });
</script>


</body>
</html>

回答by Konstantin Petrukhnov

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.

我的案例非常接近,但想法是应该将工作日数添加到脚本中。我从 Junto 示例中将 AddWeekDays() 替换为 AddBusinessDays()。因此,当计算 minday 时,它会跳过所有非工作日。

    //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
    });
    //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
    });

回答by Jorge Arreola

Modifying the above code, for use with different days in different years, you can use...

修改上面的代码,针对不同年份不同天使用,可以使用...

    <script>
     //holidays
        var natDays = [
            [11,1,2012, 'mx'],[11,2,2012, 'mx'],[11,19,2012, 'mx'],
            [12, 12, 2012, 'mx'],[12,20,2012, 'mx'],[12,25,2012, 'mx'],[12,31,2012, 'mx'],
            [1,2,2013, 'mx'],[1,3,2013, 'mx'],[1, 4, 2013, 'mx'],[1,1,2014, 'mx'],[1,2,2014, 'mx']
        ];

        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] && date.getFullYear() == natDays[i][2]) {
                    return [false, '', 'No laboral ' + natDays[i][3]+'']; /* 'Holiday in ' + natDays[i][3] */
                }
            }
            return [true, ''];
        }

        $(function() {
            $( "#datepicker" ).datepicker({
                beforeShowDay: noWeekendsOrHolidays,
                altField: '#FI',
                dateFormat: "dd/mm/yy",
                defaultDate: '-0y',
                changeMonth: true,
                changeYear: true,
                minDate: new Date(2012, 1 - 1, 5),
                //minDate: "-1Y", 
                maxDate: "0Y",
                showWeek: true,
                firstDay: 0,
                showOn: "both",
                buttonImage: "images/calendar.gif",
                buttonText: "Seleccionar Fecha", /*Select date text */
                buttonImageOnly: true,
            });
            $(this).focus();
        });
    </script>