본문 바로가기

먹고살거/KPI

[자바스크립트]4-4.내장객체(Array)

4-4.자바스크립트 내장객체(Array)


*달력만들기

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">

<head>

<title> new document </title>

<meta http-equiv="content-type" content="text/html; charset=utf-8" />

<script type="text/javascript">

//<![CDATA[

//배열 = 하나의 변수에 여러가지 데이터를 저장


var today = new Date();

//이달의 전체 일수를 구해보세요.

var totDay = [31,28,31,30,31,30,31,31,30,31,30,31];

var nowMonth = today.getMonth();

//document.write(totDay[nowMonth] + "<br/>");

//윤년구하기

var nowYear = today.getFullYear();

if(nowYear%4 == 0 && nowYear%100 != 0 || nowYear%400 == 0){

totDay[1]=29;

}

var total = totDay[nowMonth] //30일


var firstDay=new Date(nowYear,nowMonth,1)

//요일구하기

firstYoil=firstDay.getDay();

console.log(firstYoil);//5=(금)

//행수구하기

var totRow=Math.ceil((total+firstYoil)/7);

console.log(totRow);//


//이번달 달력 구하기

var theTable = "<table border='1' >"

theTable+="<tr>"

theTable+="<th>일</th>"

theTable+="<th>월</th>"

theTable+="<th>화</th>"

theTable+="<th>수</th>"

theTable+="<th>목</th>"

theTable+="<th>금</th>"

theTable+="<th>토</th>"

theTable+="</tr>"

var num = 1

for(var row=1; row<=totRow; row++){ // 행

theTable+="<tr>";

for(var col=1; col<=7; col++){//열

if (num > total || (row ==1&&col<=firstYoil)){

theTable+="<td></td>"

}else{

theTable+="<td>"+num+"</td>"

num++;

}

}

theTable+="</tr>";

}

theTable+="</table>"


window.onload=function(){

document.getElementById("calendar").innerHTML = theTable;

}


//]]>

</script>

</head>

<body>

<div id="calendar"></div>

</body>

</html>