js 计算请假时长(去除了周六日,节假日未清除)
2017-12-26 亦皓
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="content-type" content="text/html;charset=utf-8">
<link href="./bootstrap/css/bootstrap.min.css" rel="stylesheet" media="screen">
<link href="./css/bootstrap-datetimepicker.min.css" rel="stylesheet" media="screen">
</head>
<body>
<input type="text" id="days" value=" " />
<label class="control-label">天</label>
<br><br><br><br><br>
<div class="form-group">
<label for="group_name" class="col-sm-2 control-label">开始时间</label>
<div class="input-group date form_datetime col-sm-6"
data-date-format="yyyy-mm-dd hh:ii:ss "
data-link-field="dtp_input1"
style=" padding:0px 15px;">
<input class="form-control" size="16" type="text" id="time_start">
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
</div>
<br><br><br><br>
<div class="form-group">
<label for="group_name" class="col-sm-2 control-label">结束时间</label>
<div class="input-group date form_datetime col-sm-6"
data-date-format="yyyy-mm-dd hh:ii:ss "
data-link-field="dtp_input2"
style=" padding:0px 15px;">
<input class="form-control" size="16" type="text" id="time_end" >
<span class="input-group-addon">
<span class="glyphicon glyphicon-calendar"></span></span>
</div>
<input type="hidden" id="dtp_input2" /><br/>
</div>
<br><br><br>
<button id="submit1">保存</button>
<script type="text/javascript" src="jquery/jquery-1.8.3.min.js" charset="UTF-8"></script>
<script type="text/javascript" src="bootstrap/js/bootstrap.min.js"></script>
<script type="text/javascript" src="js/bootstrap-datetimepicker.js" charset="UTF-8"></script>
<script type="text/javascript" src="js/locales/bootstrap-datetimepicker.fr.js" charset="UTF-8"></script>
<script type="text/javascript" src="h.js" charset="UTF-8"></script>
<script type="text/javascript" charset="utf-8" >
$(document).ready(function(){
$(‘.form_datetime‘).datetimepicker({
language: ‘zh-CN‘,
weekStart: 1,
todayBtn: 1,
autoclose: 1,
todayHighlight: 1,
startView: 2,
forceParse: 0,
showMeridian: 1
});
})
$(‘#submit1‘).click(function(){
leaveTime("#time_start","#time_end",caleEndFun);
})
function caleEndFun(leaveTimeObje){
$(‘#days‘).val(leaveTimeObje)
}
function leaveTime(timeStart,timeEnd,enFund){
var starttime = $(timeStart).val();
var endtime = $(timeEnd).val();
var datetime1 = new Date(starttime), datetime2 = new Date(endtime);
var date1 = datetime1.setHours(0,0,0); //开始日期,毫秒表示
var date2 = datetime2.setHours(0,0,0); //结束日期,毫秒表示
var starttimeleft=starttime.substring(0,10);//获取时间到月份
var endtimeleft=endtime.substring(0,10);
var beginDate = new Date(starttimeleft.replace(/-/g, "/"));
var endDate = new Date(endtimeleft.replace(/-/g, "/"));
var dayNum = parseInt(Math.abs(beginDate - endDate) / 1000 / 60 / 60 /24);
var startWeek = beginDate.getDay();
var starHour=Number(starttime.substring(11,13));
var endHour=Number(endtime.substring(11,13));
var travelHours = 0; //保存请假小时数
var travelTimeHours = 0;
var iNow = 0;
var dayinweek= new Date(starttime).getDay();/////开始日期是星期几
var dayendweek = new Date(endtime).getDay();//结束日期是星期几
//////根据上班时间 9:00--18:00 把小时
变换到工作时区内
if(date1 ==date2 ){//开始结束时间均在一天
if(starHour<=12 ){
if(9<= endHour && endHour<13){
travelHours =4;
}else{
travelHours =8;
}
}else if(starHour>=13 ){
travelHours =4;
}
}else{
//如果开始时间小于8点,则计算工时的时候从8点开始计算
if(starHour<9){
starHour = 9;
}
//如果开始时间大于17点,
if(starHour>18){
starHour = 18;
}
if(endHour<9){
endHour =9;
}
//1.3
if(endHour>18){
endHour = 18;
}
if(0< dayinweek && dayinweek <6 && dayendweek == 0 || 0< dayinweek && dayinweek <6 && dayendweek == 6){
travelTimeHours +=(weekendBetween(dayNum) - 1)*8;
}else if(0< dayendweek && dayendweek <6 && dayinweek == 0 || 0< dayendweek && dayendweek <6 && dayinweek == 6 ){
travelTimeHours +=(weekendBetween(dayNum) - 1)*8;
}else if(dayinweek !=6 && dayinweek !=0 && dayendweek !=6 && dayendweek !=0){
travelTimeHours +=(weekendBetween(dayNum) - 2)*8;
}
if(dayinweek !=6 && dayinweek !=0){
if(starHour>=9 && starHour<=12){
travelTimeHours+=8;
}
if(starHour>=13 && starHour<=18){
travelTimeHours+=4;
}
}
if(dayendweek !=6 && dayendweek !=0){
if(endHour>=9 && endHour<=12){
travelTimeHours+=4;
}
if(endHour>=13 && endHour<=18){
travelTimeHours+=8;
}
}
}
var timeDays = travelHours / 8;
var travelTime = travelTimeHours /8;
var travelTimeDay = timeDays + travelTime;
if(enFund){
enFund(travelTimeDay)
}
// 去除周六日 ;
function weekendBetween(dayNum){
var iNow = 0;
dayNum =dayNum +1;
for (var i=0; i<dayNum; i++) {
var firstDay = startWeek % 7;
if(firstDay==0) firstDay=7;
if (firstDay < 6) {
iNow++;
}
if (firstDay == 7) {
firstDay = 1;
}
startWeek ++;
}
return iNow;
}
}
</script>
</body>
</html>
原文地址:https://www.cnblogs.com/lcc1995/p/8118493.html