日期:2014-05-16 浏览次数:20367 次
<HTML> <HEAD> <meta http-equiv="content-type" content="text/html; charset=gb2312"><meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7" /> <script type="text/javascript"> function show(year,month) { var week = showWeekDate(year,month); document.writeln("周起始时间"+week.week1.start+"-"+week.week1.end+" 工作日"+week.week1.workDays+"<br/>"); document.writeln("周起始时间"+week.week2.start+"-"+week.week2.end+" 工作日"+week.week2.workDays+"<br/>"); document.writeln("周起始时间"+week.week3.start+"-"+week.week3.end+" 工作日"+week.week3.workDays+"<br/>"); document.writeln("周起始时间"+week.week4.start+"-"+week.week4.end+" 工作日"+week.week4.workDays+"<br/>"); document.writeln("周起始时间"+week.week5.start+"-"+week.week5.end+" 工作日"+week.week5.workDays+"<br/>"); if(week.hasOwnProperty("week6")) { document.writeln("周起始时间"+week.week6.start+"-"+week.week6.end+" 工作日"+week.week6.workDays+"<br/>"); } } function showWeekDate(year,month) { var date = new Date(); if (year.length > 0 && month.length > 0 ) { date = new Date(year,month-1,1); } else { date = new Date(date.getFullYear(),date.getMonth(),1); } var week = new Object; week.week1 = new Object; week.week2 = new Object; week.week3 = new Object; week.week4 = new Object; week.week5 = new Object; //本月第一天是周几 week.today = date.getDay(); if (week.today == 0) { date.setDate(date.getDate()+1); week.today = date.getDay(); } //本月第一周工作日 week.week1.workDays = 5-week.today+1; if (week.week1.workDays<0) week.week1.workDays=0; //本月第一周起始日期 week.week1.start = date.getDate()+"/"+(date.getMonth()+1); //本月第一周结束日期 date.setDate(date.getDate()+(1+week.week1.workDays)); week.week1.end = date.getDate()+"/"+(date.getMonth()+1); //本月第二周起始日期 date.setDate(date.getDate()+1); week.week2.workDays = 5; week.week2.start = date.getDate()+"/"+(date.getMonth()+1); //本月第二周结束日期 date.setDate(date.getDate()+(1+week.week2.workDays)); week.week2.end = date.getDate()+"/"+(date.getMonth()+1); //本月第三周起始日期 date.setDate(date.getDate()+1); week.week3.workDays = 5; week.week3.start = date.getDate()+"/"+(date.getMonth()+1); //本月第三周结束日期 date.setDate(date.getDate()+(1+week.week3.workDays)); week.week3.end = date.getDate()+"/"+(date.getMonth()+1); //本月第四周起始日期 date.setDate(date.getDate()+1); week.week4.workDays = 5; week.week4.start = date.getDate()+"/"+(date.getMonth()+1); //本月第四周结束日期 date.setDate(date.getDate()+(1+week.week4.workDays)); week.week4.end = date.getDate()+"/"+(date.getMonth()+1); date.setDate(date.getDate()+1); week.week5.start = date.getDate()+"/"+(date.getMonth()+1); //计算月底日期 var nextMonth = new Date(date.getFullYear(),date.getMonth()+1,1); var monthLastDay = new Date(nextMonth-86400000); date.setDate(date.getDate()+6); if (date <= monthLastDay) { week.week5.workDays = 5; week.week5.end = date.getDate()+"/"+(date.getMonth()+1); if (date < monthLastDay) { week.week6 = new Object; date.setDate(date.getDate()+1); week.week6.start = date.getDate()+"/"+(date.getMonth()+1); week.week6.end = monthLastDay.getDate()+"/"+(date.getMonth()+1); week.week6.workDays = monthLastDay.getDay(); } } else { week.week5.end = monthLastDay.getDate()+"/"+(monthLastDay.getMonth()+1); week.week5.workDays = monthLastDay.getDay(); if (week.week5.workDays >5 ) week.week5.workDays = 5; } return week; } </script> </HEAD> <body> Year:<input type="text" id="year" value="2011"/> Month:<input type="text" id="month" value="8"/> <input type="button" value="show" onclick="javascript:show(document.getElementById('year').value,document.getElementById('month').value)"/> </body> </html>
?
?运行结果:
?
?周起始时间1/8-7/8 工作日5
周起始时间8/8-14/8 工作日5
周起始时间15/8-21/8 工作日5
周起始时