2013-05-14 177 views
0

我已經嘗試了幾種方法來實現此功能,但是由於我對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> 
+0

嗯,我繼續嘗試了大約100種不同的東西,我什麼都沒有...任何幫助將不勝感激。 –

+0

顯然,我不能選擇週末,但我仍然不知道如何讓那些日子不在計算中。 –

+0

我假設我需要一個特殊的功能來查找週末的日子並將其刪除。然後,我需要改變這一行:'var difference = Math.ceil((toDate.getTime() - fromDate.getTime()+ 1)/ oneDay);'我什至不能開始做任何事用我目前的設置找到工作。 –

回答

0

成功! This page大大幫助。所以,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); 
    } 
    } 
相關問題