1

我已经尝试了几件事来让它工作,但由于我对javascript缺乏了解,没有成功。

我只希望这个小脚本能够完全按照它当前的方式工作,但我希望计算出的天数不包括周末或节假日。有什么帮助吗?

JAVASCRIPT:

    $(function() {
  $('#from').datepicker({
  showOnFocus: false,
  showTrigger: '#calImg',
  beforeShowDay: $.datepicker.noWeekends,
  pickerClass: 'noPrevNext',
    numberOfMonths: 1,
        dateFormat: 'mm-dd-yy',
        minDate: '0',
        maxDate: '+1Y',
              onSelect: function(dateStr) {
                    var min = $(this).datepicker('getDate');
                    $('#to').datepicker('option', 'minDate', min || '0');
                    datepicked();
        }});
  $('#to').datepicker({
  showOnFocus: false,
  showTrigger: '#calImg',
  beforeShowDay: $.datepicker.noWeekends,
  pickerClass: 'noPrevNext',
    numberOfMonths: 1,
        dateFormat: 'mm-dd-yy',
        minDate: '0',
        maxDate: '+1Y',
              onSelect: function(dateStr) {
                    var max = $(this).datepicker('getDate');
                    $('#from').datepicker('option', 'maxDate', max || '+1Y');
                    datepicked();                                               
       }});
});

var datepicked = function() {
var from = $('#from');
var to = $('#to');
var nights = $('#nights');

 var fromDate = from.datepicker('getDate')

    var toDate = to.datepicker('getDate')

    if (toDate && fromDate) {
    var difference = 0;
        var oneDay = 1000*60*60*24;
        var difference = Math.ceil((toDate.getTime() - fromDate.getTime() + 1) / oneDay);
        nights.val(difference);

      }
}

HTML:

      <table width="100%" border="0" cellpadding="2">
      <tr>
      <td nowrap>Vacation Request From</td>
      <td><input type="text" id="from" name="from" size="28" style="width:194px; /*Tag Style*/" value="" ><div style="display: none;"> 
<img id="calImg" src="http://www.mywebsite.com/images/calendar-green.gif" alt="Popup" class="trigger"></div> To <input type="text" id="to" name="to" size="28" style="width:194px; /*Tag Style*/" value="" ><div style="display: none;">
<img id="calImg" src="http://www.mywebsite.com/images/calendar-green.gif" alt="Popup" class="trigger"> 

</div></td>
      <td width="1">&nbsp;</td>

     </tr>
     <tr>
      <td >Total Days Requested: </td>
      <td colspan="5"><input type="text" id="nights" name="nights" style="width:50px; /*Tag Style*/" value="" readonly="readonly"></td>
     </tr>

     <tr>
      <td colspan="6">&nbsp;</td>
     </tr>
   </table>
4

1 回答 1

0

成功! 这个页面有很大帮助。因此,HTML 与上面的更新后的 jquery/javascript 相同,我正在做生意。

    $(function() {
  $('#from').datepicker({
  showOnFocus: false,
  showTrigger: '#calImg',
  beforeShowDay: $.datepicker.noWeekends,
  pickerClass: 'noPrevNext',
    numberOfMonths: 1,
        dateFormat: 'mm-dd-yy',
        minDate: '0',
        maxDate: '+1Y',
              onSelect: function(dateStr) {
                    var min = $(this).datepicker('getDate');
                    $('#to').datepicker('option', 'minDate', min || '0');
                    datepicked();
        }});
  $('#to').datepicker({
  showOnFocus: false,
  showTrigger: '#calImg',
  beforeShowDay: $.datepicker.noWeekends,
  pickerClass: 'noPrevNext',
    numberOfMonths: 1,
        dateFormat: 'mm-dd-yy',
        minDate: '0',
        maxDate: '+1Y',
              onSelect: function(dateStr) {
                    var max = $(this).datepicker('getDate');
                    $('#from').datepicker('option', 'maxDate', max || '+1Y');
                    datepicked();                                               
        }});
    });


    var datepicked = function() {
    var from = $('#from');
    var to = $('#to');
    var nights = $('#nights');

 var startDate = from.datepicker('getDate')

var endDate = to.datepicker('getDate')


// Validate input
if (endDate && startDate) {


// Calculate days between dates
var millisecondsPerDay = 86400 * 1000; // Day in milliseconds
startDate.setHours(0,0,0,1);  // Start just after midnight
endDate.setHours(23,59,59,999);  // End just before midnight
var diff = endDate - startDate;  // Milliseconds between datetime objects    
var days = Math.ceil(diff / millisecondsPerDay);

// Subtract two weekend days for every week in between
var weeks = Math.floor(days / 7);
var days = days - (weeks * 2);

// Handle special cases
var startDay = startDate.getDay();
var endDay = endDate.getDay();

// Remove weekend not previously removed.   
if (startDay - endDay > 1)         
    var days = days - 2;      

// Remove start day if span starts on Sunday but ends before Saturday
if (startDay == 0 && endDay != 6)
    var days = days - 1  

// Remove end day if span ends on Saturday but starts after Sunday
if (endDay == 6 && startDay != 0)
   var days = days - 1  

nights.val(days);
    }
    }
于 2013-05-15T22:24:18.633 回答