본문 바로가기

Programming/[Java Script]

[Java Script] 만년달력

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
<!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>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>만년 달력</title>
<script language="javascript" type="text/javascript">
var today = new Date(); // 오늘 날짜
function prevCalendar() {
// 이전 달을 today에 값을 저장하고 달력을 뿌려줍니다.
 today = new Date(today.getFullYear(), today.getMonth() - 1, today.getDate());
 buildCalendar();
}
function nextCalendar() {
// 다음 달을 today에 값을 저장하고 달력을 뿌려줍니다.
 today = new Date(today.getFullYear(), today.getMonth() + 1, today.getDate());
 buildCalendar();
}
function buildCalendar() {
 var nMonth = new Date(today.getFullYear(), today.getMonth(), 1);  // 이번 달의 첫째 날
 var lastDate = new Date(today.getFullYear(), today.getMonth()+10); // 이번 달의 마지막 날
 var tblCalendar = document.getElementById("calendar");     // 테이블 달력을 만들 테이블
 var tblCalendarYM = document.getElementById("calendarYM");    // yyyy년 m월 출력할 곳
 tblCalendarYM.innerHTML = today.getFullYear() + "년 " + (today.getMonth() + 1+ "월";  // yyyy년 m월 출력
// 기존 테이블에 뿌려진 줄, 칸 삭제
 while (tblCalendar.rows.length > 2) {
  tblCalendar.deleteRow(tblCalendar.rows.length - 1);
 }
 var row = null;
 row = tblCalendar.insertRow();
 var cnt = 0;
// 1일이 시작되는 칸을 맞추어 줌
 for (i=0; i<nMonth.getDay(); i++) {
  cell = row.insertCell();
  cnt = cnt + 1;
 }
// 달력 출력
 for (i=1; i<=lastDate.getDate(); i++) { // 1일부터 마지막 일까지
  cell = row.insertCell();
  cell.innerHTML = i;
  cnt = cnt + 1;
  if (cnt%7 == 0)       // 1주일이 7일 이므로
   row = calendar.insertRow();   // 줄 추가
 }
}
</script>
</head>
<body>
<table id="calendar" boarder="3">
 <tr>
     <td><label onclick="prevCalendar()"><</label></td>
        <td colspan="5" align="center" id="calendarYM">yyyy년 m월</td>
        <td><label onclick="nextCalendar()">></label></td>
    </tr>
    <tr>
     <td align="center">일</td>
     <td align="center">월</td>
     <td align="center">화</td>
     <td align="center">수</td>
     <td align="center">목</td>
     <td align="center">금</td>
     <td align="center">토</td>
    </tr>
</table>
<script language="javascript" type="text/javascript">
 buildCalendar();
</script>
</body>
</html>
cs