当前位置: 代码迷 >> JavaScript >> js 下拉时间段-礼拜
  详细解决方案

js 下拉时间段-礼拜

热度:604   发布时间:2013-12-21 20:16:01.0
js 下拉时间段--星期
<td style="font: normal 宋体; font-size: 9pt;" height="40">
				查询年份: 
				<input type="text" id="time" name="time" onChange="week(3)" class="Wdate" value='<%=now%>' style="font: normal 宋体; font-size: 9pt; width: 80px" onfocus="WdatePicker({readOnly:true,dateFmt:'yyyy'})" />
				&nbsp;&nbsp;&nbsp;查询时间: 
				<select id="week" value="" style="font: normal 宋体; font-size: 9pt; width: 220px" onChange="calcu(3)"></select>
				<input type="button" id="fanhui" name="fanhui" style="font: normal 宋体; font-size: 9pt;" value="查询" onClick="calcu(2);">
				<label id="label"></label>
			</td>

?
<script type="text/javascript">
	var startDate;
	var endDate;
	
	function calcu(objct) {
		var nowYear =  document.getElementById("time").value;//当前年
		//-------------------------------------------------------------------------------------
		var yearStart = new Date(parseInt(nowYear), 0, 1); //设置该年1.1.
		var firstDay = yearStart.getDay(); //星期
		var yearEnd = new Date(parseInt(nowYear), 11, 31); //设置该年12.31.
		var endDay = yearEnd.getDay(); //星期
		//-------------------------------------------------------------------------------------    
		//判断输入是否超过最大周次.
		var maxWeek = 52; //该年最大周次.1.1.在周一到周四,则为该年第一周,否则为上年最后一周.
		if ((firstDay >= 1 && firstDay <= 4)
				&& (endDay == 0 || endDay == 4 || endDay == 5 || endDay == 6)) {
			maxWeek = 53; //1.1.和12.31.都在本年,则该年有53周,否则52周
		} else {
			maxWeek = 52;
		}
		//----------------------------------------------------------------------------------------

		//-------------------------------------------------------------------------------------
		//对1.1.所在周的前后几天特殊处理.
		if (firstDay >= 0 && firstDay <= 4) {
			other = firstDay - 1;
		} else {
			other = firstDay - 8;
		}
		//-------------------------------------------------------------------------------------

		//-------------------------------------------------------------------------------------
		//时间调整,得出要计算周的起/始时间.    
		//距离当年1.1.的总天数
		days = (parseInt(document.getElementById("week").value, 10) - 1) * 7
				- other;
		//转换成Ms.......
		var oneMinute = 60 * 1000;
		var oneHour = oneMinute * 60;
		var oneDay = oneHour * 24;
		//1.1.至1/1/70的毫秒数
		var dateInMs = yearStart.getTime();
		//当前所选周第一天离1/1/70的毫秒数.
		dateInMs += oneDay * days;
		//日期调整(设置1/1/70至今的毫秒数)
		yearStart.setTime(dateInMs);
		//当前所选周最后一天处理,同上.
		var weekEnd = new Date(parseInt(nowYear), 0, 1);
		var dateInMs1 = weekEnd.getTime();
		dateInMs1 += oneDay * (days + 6);
		weekEnd.setTime(dateInMs1);
		//-------------------------------------------------------------------------------------

		//-------------------------------------------------------------------------------------
		//月和日的处理,一位变两位,如:1->01.    
		var month = yearStart.getMonth() + 1;
		if (month < 10) {
			month = "0" + month;
		}
		var day = yearStart.getDate();
		if (day < 10) {
			day = "0" + day;
		}
		var month1 = weekEnd.getMonth() + 1;
		if (month1 < 10) {
			month1 = "0" + month1;
		}
		var day1 = weekEnd.getDate();
		if (day1 < 10) {
			day1 = "0" + day1;
		}
		//-------------------------------------------------------------------------------------
		startDate = nowYear + "-" + month + "-" + day;
		endDate = nowYear + "-" + month1 + "-" + day1;
		if(startDate > endDate){
			newNowYear = nowYear-1;
			startDate = newNowYear + "-" + month + "-" + day;
		}
		
		document.getElementById("label").innerHTML = "您选择的时间为:" + startDate
				+ " ~  " + endDate;
		submit(objct);
		//document.getElementById('Ifrmname1').src="http://localhost:8080<%=request.getContextPath()%>/ReportEmitter?rpt=/CISS/11.brt&params=startData="+startDate+";endData="+endDate;
		//-------------------------------------------------------------------------------------
	}
	
	
	function week(objct) {
		var sDate;
		var eDate;
		var dateInMs2;
		var nowYear = document.getElementById("time").value;
		//-------------------------------------------------------------------------------------

		var yearStart = new Date(parseInt(nowYear), 0, 1); //设置该年1.1.
		var firstDay = yearStart.getDay(); //星期
		var yearEnd = new Date(parseInt(nowYear), 11, 31); //设置该年12.31.
		var endDay = yearEnd.getDay(); //星期
		//-------------------------------------------------------------------------------------    
		//判断输入是否超过最大周次.
		var maxWeek; //该年最大周次.1.1.在周一到周四,则为该年第一周,否则为上年最后一周.
		if ((firstDay >= 1 && firstDay <= 4)
				&& (endDay == 0 || endDay == 4 || endDay == 5 || endDay == 6)) {
			maxWeek = 53; //1.1.和12.31.都在本年,则该年有53周,否则52周
		} else {
			maxWeek = 52;
		}
		

		//对1.1.所在周的前后几天特殊处理.
		if (firstDay >= 0 && firstDay <= 4) {
			other1 = firstDay - 1;
		} else {
			other1 = firstDay - 8;
		}
		
		//给select赋值
		var week = document.getElementById("week");
		week.options.length = 0;
		for ( var i = 1; i <= maxWeek; i++) {
			
			var days1 = (parseInt(i, 10) - 1) * 7 - other1;
			//转换成Ms.......
			var oneMinute = 60 * 1000;
			var oneHour = oneMinute * 60;
			var oneDay = oneHour * 24;
			//1.1.至1/1/70的毫秒数
			var dateInMs;
			//当前所选周第一天离1/1/70的毫秒数.
			if( i == 1){
				dateInMs = yearStart.getTime();
				dateInMs += oneDay * days1;
				dateInMs2 = yearStart.getTime();
				yearStart.setTime(dateInMs);
			}else{
				var dateInMs3 = dateInMs2 + oneDay * days1;
				yearStart.setTime(dateInMs3);
			}
			//日期调整(设置1/1/70至今的毫秒数)
			//yearStart.setTime(dateInMs);
			//当前所选周最后一天处理,同上.
			var weekEnd = new Date(parseInt(nowYear), 0, 1);
			var dateInMs1 = weekEnd.getTime();
			dateInMs1 += oneDay * (days1 + 6);
			weekEnd.setTime(dateInMs1);
			//-------------------------------------------------------------------------------------
		
			//-------------------------------------------------------------------------------------
			//月和日的处理,一位变两位,如:1->01.    
			var month = yearStart.getMonth() + 1;
			if (month < 10) {
				month = "0" + month;
			}
			var day = yearStart.getDate();
			if (day < 10) {
				day = "0" + day;
			}
			var month1 = weekEnd.getMonth() + 1;
			if (month1 < 10) {
				month1 = "0" + month1;
			}
			var day1 = weekEnd.getDate();
			if (day1 < 10) {
				day1 = "0" + day1;
			}
			//-------------------------------------------------------------------------------------
			sDate = nowYear + "-" + month + "-" + day;
			eDate = nowYear + "-" + month1 + "-" + day1;
			
			if(sDate > eDate){
				newNowYear = nowYear-1;
				sDate = newNowYear + "-" + month + "-" + day;
			}
			var y = document.createElement("option");
			y.text = "第" + i + "周 ("+sDate+"~"+eDate+")";
			y.value = i;
			try {
				week.add(y, null);
			} catch (ex) {
				week.add(y);
			}
		}
		var d1 = new Date();
		var d2 = new Date();
		d2.setMonth(0);
		d2.setDate(1);
		var rq = d1 - d2;
		//一年的第多少天
		var s1 = Math.ceil(rq / (24 * 60 * 60 * 1000));
		//一年中的某一周
		var s2 = Math.ceil(s1 / 7);
		document.getElementById('week').value = s2;
		//document.getElementById('week').innerHtml = "第" + s2 + "周()";
		calcu(objct);
		//-->
	}

	
	function submit(objct) {
		if(objct==3){
			
		}else{
			document.getElementById('Ifrmname1').src = "<%=request.getContextPath()%>/ReportEmitter?rpt=/cmwp/weeklyReport.brt&params=start_date="
				+ startDate + ";end_date=" + endDate + ";orgid= " + '<%=orgid%>' + ";plan_type=W";
		}
	}
	
	
//-->
</script>

?实现查询时间区间--以星期分开

  相关解决方案