1

I've tried several things to get this to work, but because of my lack of understanding of javascript, no success.

I just want this little script to work exactly how it currently does, but I want the calculated days to have no weekends or holidays included in the sum. Any help?

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>
cfs
  • 10,610
  • 3
  • 30
  • 43
Travis Thomas
  • 13
  • 1
  • 5
  • Well, I've continued to try about 100 different things and I've got nothing... Any help would be greatly appreciated. – Travis Thomas May 14 '13 at 23:24
  • Obviously, I am able to not have weekends be selected, but I still can't figure out how to have those days not be in the calculation. – Travis Thomas May 14 '13 at 23:27
  • I'm assuming I need a special function to find the weekend days and remove them. Then I'd need to change this line: `var difference = Math.ceil((toDate.getTime() - fromDate.getTime() + 1) / oneDay);` I can't even begin to make anything I'm finding work with my current setup. – Travis Thomas May 14 '13 at 23:28
  • This question may help: http://stackoverflow.com/questions/16213333/calculate-number-of-days-between-two-dates-excluding-sundays-and-an-array-of-hol – mccannf May 15 '13 at 21:24

1 Answers1

0

Success! This page greatly helped. So, HTML is the same as above along with this updated jquery/javascript and I'm in business.

    $(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);
    }
    }
Travis Thomas
  • 13
  • 1
  • 5