计算假期总数,不包括节假日和周末

时间:2019-02-13 06:46:14

标签: jquery html datepicker

我有一个程序可以计算不包括周末和国定假日的总休假天数。我已经成功地排除了周末,但我仍然对忽略国定假日感到困惑

该程序是使用html和jquery datepicker创建的,

这是JavaScript代码

var natDays = [
    [12, 25, 'id']
]; 
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 noWeekendsOrHolidays(date) {
    var noWeekend = $.datepicker.noWeekends(date);
    if (noWeekend[0]) {
        return nationalDays(date);
    } else {
        return noWeekend;
    }
}

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

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

var datepicked = function() {

var from = $('#from');
var to = $('#to');
var vacation = $('#vacation');

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  

vacation.val(days);
}
}
</script>

这是html代码

<input type="text" id="from" name="from"><br>
<input type="text" id="to" name="to"><br>
<input type="text" id="vacation" name="vacation"> <br/>

当我单击时(例如,星期五至星期一),它可以计算2个假期,但是当我想从星期日至星期五休假时,在星期三有一个假期,例如圣诞节,它仍然算作5个假期< / p>

1 个答案:

答案 0 :(得分:0)

最后,我可以使用sql查询来实现

我正在使用SQL制作假期表,然后进行处理以与日期之间和日期之间有假期的用户输入进行匹配