JS 작업 날짜, 지난주 와 다음 주 실현

5244 단어 JS 작업 날짜
JS 작업 날짜, 지난주 와 다음 주 날짜 전환 실현.
 
<html>
<head>
<script>  
var currDT; 
var aryDay = new Array(" "," "," "," "," "," "," ");//      
var lastDay;//         
var firstDay;//        

//       
function initDate() {
	currDT = new Date();
	showdate.innerHTML = currDT.toLocaleDateString(); //      
	var dw = currDT.getDay();// Date           (0~6)
	var tdDT;//    

	//             
	var objTB = document.getElementById("mytable");//      
	for(var i=0;i<7;i++) {
		tdDT = getDays()[i];
		if(tdDT.toLocaleDateString() == currDT.toLocaleDateString()) {  
			objTB.rows[0].cells[i].style.color = "red";//currDT      
		}  
		dw = tdDT.getDay();//   
		objTB.rows[0].cells[i].innerHTML = tdDT.getMonth()+1 + " " + tdDT.getDate() + "    " + aryDay[dw];//   
	} 
	//    
	lastDay = getDays()[6];//       
	firstDay = getDays()[0];//      
}

//             
function getWeek(i) {
	var now = new Date();
	var n = now.getDay();
	var start = new Date();
	start.setDate(now.getDate() - n + i);//         、   、   ...
	return start;
}

//            
function getDays() {
	var days = new Array();
	for(var i=1;i<=7;i++) {
		days[i-1] = getWeek(i);
	}
	return days;
}

//         (   )
function getNextWeekDatas(ndt) {
	var days = new Array();
	for(var i=1;i<=7;i++) {
		var dt = new Date(ndt);
		days[i-1] = getNextWeek(dt,i);
	}
	return days;
}

//        (   )
function getNextWeek(dt,i) {
	var today = dt;
	today.setDate(today.getDate()+i);
	return today;
}


//         (   )
function getPreviousWeekDatas(ndt) {
	var days = new Array();
	for(var i=-7;i<=-1;i++) {
		var dt = new Date(ndt);
		days[7+i] = getPreviousWeek(dt,i);
	}
	return days;
}

//        (   )
function getPreviousWeek(dt,i) {
	var today = dt;
	today.setDate(today.getDate()+i);
	return today;
}

//   
function nextWeek() {
	setCurrDTAfter();//    
	showdate.innerHTML = currDT.toLocaleDateString(); //      

	//             
	var objTB = document.getElementById("mytable");//      
	var dw = currDT.getDay();// Date           (0~6)
	var tdDT;//  
	
	for(var i=0;i<7;i++) {
		tdDT = getNextWeekDatas(lastDay)[i];
		if(tdDT.toLocaleDateString()==currDT.toLocaleDateString()) {  
			objTB.rows[0].cells[i].style.color = "red";//currDT      
		}  
		dw = tdDT.getDay();//   
		objTB.rows[0].cells[i].innerHTML = tdDT.getMonth()+1 + " " + tdDT.getDate() + "    " + aryDay[dw]; //   
	} 
	//    
	firstDay = getNextWeekDatas(lastDay)[0];//      1
	lastDay = getNextWeekDatas(lastDay)[6];//      2
}

//   
function previousWeek() {
	settCurrDTBefore();
	showdate.innerHTML = currDT.toLocaleDateString(); //      

	//             
	var objTB = document.getElementById("mytable");//      
	var dw = currDT.getDay();// Date           (0~6)
	var tdDT;//  

	for(var i=0;i<7;i++) {
		tdDT = getPreviousWeekDatas(firstDay)[i];
		
		if(tdDT.toLocaleDateString()==currDT.toLocaleDateString()) {  
			objTB.rows[0].cells[i].style.color = "red";//currDT      
		}  
		dw = tdDT.getDay();//   
		objTB.rows[0].cells[i].innerHTML = tdDT.getMonth()+1 + " " + tdDT.getDate() + "    " + aryDay[dw];//  
	} 
	//    
	lastDay =  getPreviousWeekDatas(firstDay)[6];//      1
	firstDay = getPreviousWeekDatas(firstDay)[0];//      2
}

//        
function setCurrDTAfter() {
	currDT.setDate(currDT.getDate()+7);
}

//        
function settCurrDTBefore() {
	currDT.setDate(currDT.getDate()-7);
}
</script>
</head>
<body onload="initDate()">
<table align="center">
	<tr>  
		<td>
			<input type="button" name="previousweek" value="   " onclick="previousWeek();"/>
			<span id="showdate"></span>
			<input type="button" name="previousweek" value="   " onclick="nextWeek();"/>
		</td>
	</tr>  
</table>
<!--    -->
<table id="mytable" cellspacing="0" border=="1" align="center" width="90%">
	<tr>  
		<td></td>  
		<td></td>  
		<td></td>  
		<td></td>  
		<td></td>  
		<td></td>  
		<td></td>  
	</tr>  
</table>
 </body>
</html>

좋은 웹페이지 즐겨찾기