0

我需要将用户定义的工作日添加到用户定义的日期并使其显示在警报框中。

您能否查看我的代码并让我知道需要更改的内容:

JSFiddle

Select Business Days To Add<br>
<select name="coll" id="t1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select><p>

  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <script>  $(function() {    $( "#datepicker" ).datepicker(); }); </script>

<input id="datepicker" onchange="alert()"/>
4

1 回答 1

0

首先编写一个获取所选日期和日期的函数:

<center>Select Business Days To Add<br>
<select name="coll" id="t1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select><p>

  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <script>  $(function() {    $( "#datepicker" ).datepicker(); });
    function getSelectedDate() {
           var day = $("#datepicker").datepicker("getDate");     
           var daysToAdd = $("#t1").val();
           return Number(daysToAdd) + Number(day.getDate());
        }
    </script>

<input id="datepicker" onchange="alert(getSelectedDate());"/>

我想工作日的意思是从星期一到星期五,所以在星期五加 1 意味着实际上是星期一而不是星期六。

更远

  • 您需要查看当前日期是否为星期五,并在所选工作日中添加两天以跳过周末 - 使用getDate()
  • 如果是月底(关于偶数和不均匀月份 30 和 31 天以及 2 月闰年而不是闰年)并且如果需要也增加月份 - 使用getMonth
  • 如果是年底,则增加年份 - 使用getFullYear

代码(不检查周末)如下所示:

<center>Select Business Days To Add<br>
<select name="coll" id="t1">
<option value="1">1</option>
<option value="2">2</option>
<option value="3">3</option>
</select><p>

  <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
    <script>  $(function() {    $( "#datepicker" ).datepicker(); });

        function isEndOfMonth(date) {
        // day of month from 1 to 31
        var selectedDay = date.getDate();
        // months with 31 days
        if (date.getMonth() == 0 || date.getMonth() == 2 || 
            date.getMonth() == 4 || date.getMonth() == 6 || 
            date.getMonth() == 7 || date.getMonth() == 9 || 
            date.getMonth() == 11) {
            return selectedDay == 31;
        }
        // february 28 / 29 days TODO check for leap year!
        if (date.getMonth() == 1) { return selectedDay == 28; }

        // months with 30 days
        if (date.getMonth() == 3 || date.getMonth() == 5 || 
            date.getMonth() == 8 || date.getMonth() == 10) {
            return selectedDay == 30;
        }
            return false;
        }

        function isEndOfYear(month) {
            return month > 11;
        }

        function getSelectedDate() {
           var day = $("#datepicker").datepicker("getDate");

           var daysToAdd = Number($("#t1").val());
           // values from which the new date is constructed
           var selectedMonth = day.getMonth();
           var selectedYear = day.getFullYear();
           var selectedDay = day.getDate();
           // 
           if (isEndOfMonth(day)) {
               // start new month
               selectedDay = 1;
               selectedMonth++;
               if (isEndOfYear(selectedMonth)) {
                   // start new year
                   selectedYear++;
               }
           }
            // add business days
            selectedDay += daysToAdd;
            // TODO check if the result is weekend and jump over it. After a jump the same checks as above should be called!
            return new Date(selectedYear, selectedMonth, selectedDay, 0, 0, 0, 0);
        }
    </script>

<input id="datepicker" onchange="alert(getSelectedDate());"/>

例如,如果选择的日期是14.Oct.2013,选择的天数是2,则该函数产生16.Oct.2013

于 2013-10-09T21:11:48.987 回答