JS端计算一段时间内工作日的天数,排除周日和法定节假日,由于每年的法定假日不一样所以应该由管理员导入数据库中,通过ajax异步加载到js中进行判断。
<script type="text/javascript">
$(document).ready(function() {
udf_field.init();
$("#confirm span").on("dblclick", function() {
$("#confirm span").last().find("b").remove();
});
$("#consult span").on("dblclick", function() {
$("#consult span").last().find("b").remove();
});
if($("#flow_type_name").val()=="事假申请"||$("#flow_type_name").val()=="病假申请"||$("#flow_type_name").val()=="加班申请"){
// $("#yang").css("display","block");
$("#start_time").attr("check","require");
$("#end_time").attr("check","require");
}
});
// add yatao.xu start
/*增加天数*/
function addDays(oDate,days){
if(days > 0){
days = days - 1;
}
if(days < 0){
days = days + 1;
}
var result = new Date(oDate.getTime()+(days*24 * 60 * 60 * 1000));
return result;
}
// 对Date的扩展,将 Date 转化为指定格式的String
// 月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符,
// 年(y)可以用 1-4 个占位符,毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
// 例子:
// (new Date()).Format("yyyy-MM-dd hh:mm:ss.S") ==> 2006-07-02 08:09:04.423
// (new Date()).Format("yyyy-M-d h:m:s.S") ==> 2006-7-2 8:9:4.18
Date.prototype.Format = function(fmt) {
var o = {
"M+" : this.getMonth()+1, //月份
"d+" : this.getDate(), //日
"h+" : this.getHours(), //小时
"m+" : this.getMinutes(), //分
"s+" : this.getSeconds(), //秒
"q+" : Math.floor((this.getMonth()+3)/3), //季度
"S" : this.getMilliseconds() //毫秒
};
if(/(y+)/.test(fmt))
fmt=fmt.replace(RegExp.$1, (this.getFullYear()+"").substr(4 - RegExp.$1.length));
for(var k in o)
if(new RegExp("("+ k +")").test(fmt))
fmt = fmt.replace(RegExp.$1, (RegExp.$1.length==1) ? (o[k]) : (("00"+ o[k]).substr((""+ o[k]).length)));
return fmt;
};
function nearlyWeeks (weekcount, end) {
//功能:计算当前时间(或指定时间),向前推算周数(weekcount),得到结果周的第一天的时期值;
var overtime=new Date(end).Format("yyyy-MM-dd");
var overTime=new Date(end);
var days=0 ;
days=(overTime.getDay() == 0 ? 7 : overTime.getDay());
var ss=(days + weekcount * 7) * 24 * 60 * 60 * 1000;
return new Date(end - (days + weekcount * 7) * 24 * 60 * 60 * 1000);
};
function getWorkDayCount (beginDay, endDay, Holiday) {
/*
功能:计算一段时间内工作的天数。不包括周末和法定节假日,法定调休日为工作日,周末为周六、周日两天;
参数:
beginDay -时间段开始日期;
endDay -时间段结束日期;
*/
//每天的毫秒总数,用于以下换算
var daytime = 24 * 60 * 60 * 1000;
//两个时间段相隔的总天数
if(parseInt((endDay - beginDay) / 1000 / 60 / 60 /24)<(endDay - beginDay) / 1000 / 60 / 60 /24){
var days =parseInt((endDay - beginDay) / 1000 / 60 / 60 /24)+1;
}else{
days = parseInt((endDay - beginDay) / 1000 / 60 / 60 /24);
}
//减去不用上班的时间,即09-18之外的时间
//日期格式为yyyy-mm-dd
if(beginDay.getHours<10 ){
beginDay.getHours=10;
}
if(endDay.getHours>18){
endDay.getHours=18
}
//剩余的小时数
var hours= endDay.getHours() - beginDay.getHours();
// ui_error("剩余的小时数:"+ hours);
var tempDate = beginDay;
while(tempDate.getTime() <= endDay.getTime()){
tempDate = addDays(tempDate,2);//加一天
}
if(hours>=4) {
days+=0.5;
}
//时间段起始时间所在周的第一天
var beginWeekFirstDay=nearlyWeeks(0,beginDay.getTime()).getTime();
//时间段结束时间所在周的最后一天
var endWeekOverDay=nearlyWeeks(0,endDay.getTime()).getTime()+6*daytime;
//由beginWeekFirstDay和endWeekOverDay换算出,周末的天数
var weekEndCount = parseInt(((endWeekOverDay - beginWeekFirstDay) / daytime + 1) / 7 * 2-2);
//调整周末天数的值
if(weekEndCount!=0){
if(endDay.getDay()>0 && endDay.getDay<6)
weekEndCount -=2;
if (endDay.getDay() == 6)
weekEndCount -= 1;
if (beginDay.getDay() == 0) weekEndCount -= 1;
}
//根据法定假日设置,计算时间段内周末的天数(包含法定假日)
var Holidays=new Array();
for (var i =0 ;i <= Holiday.length - 1; i++) {
Holidays[i]=Holiday[i].holiday_date;
}
$.each(Holidays, function (i, itemHoliday) {
var itemDay=new Date(itemHoliday);
//如果法定假日在时间段区间内,且为工作日时间(周一至周五),周末天数值+1
if (itemDay.getTime() >= beginDay.getTime() && itemDay.getTime() <= endDay.getTime() && itemDay.getDay() > 0 && itemDay.getDay() < 6)
weekEndCount += 1;
});
//工作日 = 总天数 - 周末天数(包含法定假日并排除调休日)
var day=days-weekEndCount;
ui_error("请假天数:"+day);
return day;
};
function holiday(begintime,endtiem){
sendAjax("{:U('BookMeetingRoom/select_holiday')}",null,function(data){
var holiday=data.holiday_date;
getWorkDayCount(begintime,endtiem,holiday);
});
}
function save(step) {
var oDate2 = new Date($("#end_time").val().replace(/\-/g, "\/"));
var oDate1 = new Date($("#start_time").val().replace(/\-/g, "\/"));
if($("#flow_type_name").val()=="事假申请"||$("#flow_type_name").val()=="病假申请"||$("#flow_type_name").val()=="加班申请"){
if(oDate2<=oDate1){
ui_error("结束时间,需大于开始时间");
return;
}
if($("#flow_type_name").val()=="事假申请"||$("#flow_type_name").val()=="病假申请"){
holiday(oDate1,oDate2);
}
}
window.onbeforeunload = null;
$("#confirm").val("");
$("#confirm_wrap span").each(function() {
$("#confirm").val($("#confirm").val() + $(this).attr("data") + '|');
});
$("#confirm_name").val($("#confirm_wrap").html());
$("#consult").val("");
$("#consult_wrap span").each(function() {
$("#consult").val($("#consult").val() + $(this).attr("data") + '|');
});
$("#consult_name").val($("#consult_wrap").html());
$("#refer").val("");
$("#refer_wrap span").each(function() {
$("#refer").val($("#refer").val() + $(this).attr("data") + '|');
});
$("#refer_name").val($("#refer_wrap").html());
if ($("#confirm").val().length < 2) {
ui_error('请选择审批流程');
return false;
}
$("#step").val(step);
sendForm("form_data", "{:U('save')}");
}
function popup_confirm() {
winopen("{:U('popup/confirm')}", 704, 570);
}
</script>

在数据中把12-01设成法定假日