JS計算請假時間(按每天7.5小時)
阿新 • • 發佈:2018-12-21
function getTotal(beginTime ,endTime){ var days; var hours; var date; var stWorkTime=8; var enWrokTime=17; var freeTimeMon=12; var freeTimeAft=13.5; var freeTime=freeTimeMon-freeTimeMon; var beginArr = beginTime .split(" "); var beginMonth = parseInt( beginArr[0].split("-")[1]); var beginDay = parseInt(beginArr[0].split("-")[2]); var beginHours = parseInt(beginArr[1].split(":")[0]); var beginMin = parseInt(beginArr[1].split(":")[1]); var endArr = endTime.split(" "); var endMonth = parseInt(endArr[0].split("-")[1]); var endDay = parseInt(endArr[0].split("-")[2]); var endHours = parseInt(endArr[1].split(":")[0]); var endMin = parseInt(endArr[1].split(":")[1]); //同一個月 if(beginMonth==endMonth){ //未跨天 if(beginDay ==endDay){ //stWorkTime-enWrokTime if((beginHours==stWorkTime) &&(endHours==enWrokTime)){ days=1; hours=0; } //stWorkTime-enWrokTime之間 else if((beginHours>=stWorkTime)&&(endHours<=enWrokTime)){ //跨午休 if(((beginHours>=stWorkTime)&&(beginHours<freeTimeMon))&&((endHours>freeTimeAft)&&(endHours<enWrokTime))){ hours=endHours-beginHours-freeTime; days=0; } //未跨午休 else if( ((beginHours>=stWorkTime)&&(endHours<=freeTimeMon))||((beginHours>=freeTimeAft)&(endHours<=enWrokTime))){ hours =endHours-beginHours; days=0; } } } //跨天 else{ var tag =endDay-beginDay; var stdate; var endate; //第一天 if(beginHours>=stWorkTime&&beginHours<=freeTimeMon){ stdate=enWrokTime-beginHours-freeTime; }else if(beginHours>=freeTimeAft&&beginHours<=enWrokTime){ stdate=enWrokTime-beginHours; } //最後一天 if(endHours>=stWorkTime&&endHours <=freeTimeMon){ endate= endHours-stWorkTime; }else if(endHours>=freeTimeAft&&endHours<=enWrokTime){ endate= enWrokTime-endHours-freeTime; } hours = stdate+endate; endDay-beginDay >1? days=getDays(beginTime,endTime)-1:days=getDays(beginTime,endTime); if(hours%7.5>0){ days+=1; hours=hours%7; } } } date ={"days":days,"hours":hours} return date; } var date = getTotal(beginTime,endTime); $("#xxx").val(date["days"]); $("#yyy").val(date["hours"]); //判斷開始時間小於結束時間 function checkDate(beginTime ,endTime){ var start = new Date(beginTime.replace("-", "/").replace("-", "/")); var end = new Date(endTime.replace("-", "/").replace("-", "/")); if (end < start) { alert('結束日期不能小於開始日期!'); return false; } else{ return true; } } //兩個日期之間的天數 function getDays(strDateStart,strDateEnd){ var strSeparator = "-"; //日期分隔符 var oDate1; var oDate2; var iDays; oDate1= strDateStart.split(strSeparator); oDate2= strDateEnd.split(strSeparator); var strDateS = new Date(oDate1[0] + "-" + oDate1[1] + "-" + oDate1[2]); var strDateE = new Date(oDate2[0] + "-" + oDate2[1] + "-" + oDate2[2]); iDays = parseInt(Math.abs(strDateS - strDateE ) / 1000 / 60 / 60 /24) return iDays ; }