计算工作日

时间:2015-03-15 03:07:53
【文件属性】:

文件名称:计算工作日

文件大小:2KB

文件格式:TXT

更新时间:2015-03-15 03:07:53

js 计算工作日

js两个日期之间的工作日数 [removed] //假日表 var holidays = new Array(); holidays[holidays.length] = new Date("2012/02/01"); holidays[holidays.length] = new Date("2012/02/03"); function getWorkDays(){ var dd = document.getElementById("days"); dd.value=calculateWorkDays(document.getElementById("dateFrom").value,document.getElementById("dateTo").value,holidays); } function calculateWorkDays(fromStr, toStr,holidayArray){ var from = new Date(); var to = new Date(); var fromTime = Date.parse(fromStr); var toTime = Date.parse(toStr); from.setTime(fromTime); from.setHours(0); from.setMinutes(0); from.setSeconds(0); var orgTime = from.getTime(); to.setTime(toTime); to.setHours(0); to.setMinutes(0); to.setSeconds(0); if(from.getTime() > to.getTime()){ return 0; } //把起始日都修正到星期六 javascript的星期从0开始,所以+1来处理 var sDayofWeek = from.getDay()+1; var workdays=0; //修正到星期六之后,再修正多出来的非假日 if(sDayofWeek > 1 && sDayofWeek < 7) { from.setDate(from.getDate()-(sDayofWeek%7)); workdays-=((sDayofWeek-2)>0)?sDayofWeek-2:0; } //计算两个日期之间的天数 var totalDays = (to.getTime()-from.getTime())/(1000*60*60*24)+1; workdays+=Math.floor(totalDays/7)*5; alert(workdays) //修正最后剩余天数 if((totalDays%7-2)>0){ workdays+=(totalDays%7-2); } if(holidayArray!=null){ for(var i=0;iorgTime && holidayArray[i].getTime()0 && holidayArray[i].getDay()<6){ workdays--; } } } } return workdays; } [removed]


网友评论