views:

1286

answers:

1

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 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 this to JavaScript?

+1  A: 

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"&gt;&lt;/script&gt; 
        <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jqueryui/1.7.1/jquery-ui.min.js"&gt;&lt;/script&gt; 
        <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>
Junto