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

前端 未结 3 1610
花落未央
花落未央 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 11:41

    Found the solution here.

    Code (apologies for the 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>
    
    0 讨论(0)
  • 2020-12-28 11:54

    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>
    
    0 讨论(0)
  • 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
        });
    

    0 讨论(0)
提交回复
热议问题