본문 바로가기

Programming/HTML&JAVASCRIPT&JQuery

<JAVASCRIPT>만년달력 만들기

728x90
반응형





 지속적으로 사용할 수 있는 달력을 만들어 보았습니다.

수정할 부분이 있으면 답글달아주시면 바로 수정하겠습니다.




(코드 내용)

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
<!DOCTYPE html>
<html>
<head>
    <title>만년 달력</title>
    <script language="javascript">
        function changeColor()
    {
        var color = ["#FF0000""#00FF00""#0000FF","#000FFF","#00F0FF","#0F00FF","#F000FF","#F002FF","#F110FF"];
        var title = document.getElementById("body");
        var i = 0;
 
        setInterval(function(){
        title.style.color = color[i]; //title의 색상을 바꿈
        i = (i+1) % color.length;
    }, 50);
    }
    </script>
    <script language="javascript">
        var today = new Date();
        var year = today.getFullYear();
        var month = today.getMonth();
        var day = today.getDay();
 
        month += 1;
 
        function dayy(year, month){ //월의 일수를 구함
            switch(month){
                case 1case 3case 5case 7case 8case 10case 12:
                return 31;
 
                case 4case 6case 9case 11:
                return 30;
 
                case 2:
                if(((year%400)==0||(year%4)==0&&(year%100)!=0)){
                    return 29;
                }
                else{
                    return 28;
                }
            }
            
        } 
 
        
        
    
        function prevmonth(){ //이전 월로 가는 함수
        
        var ymda = document.getElementById("prev");
        var yg = document.getElementById("Ymd");
 
        month--//month를 계속 감소시켜준다
        if(month < 1){ // month가 1보다 작아지면
            month = 12// month를 12로 만들어줌
            year -= 1//year를 1 감소시켜준다
        }
        if(year < 1970){ //1970년 밑으로는 내려가지 않음..
            alert("기원전");
             for(var i=1;i<100;i--){
            window.top.moveTo(i ,i *=-1);
            }
        }
 
        var ymda = year + "년" + (month)+"월";
    
        present();
        } 
 
        function nextmonth(){  //다음 월로 가는 함수
              
        var ymda = document.getElementById("next");
        var yg = document.getElementById("Ymd");
 
        month++//month 를 계속 증가시켜줌
        if(month > 12){ //만약 month가 12를 넘어가면
            month = 1// month를 1로 만듦
            year += 1//year을 1 증가시켜준다
        }
 
        var ymda = year + "년" + month+"월";
    
        present(); //present()함수를 호출하여 다시 찍어줌
 
        }
 
        function present(){
        
        var start = new Date(year, month-11);
        var ymda = document.getElementById("Ymd");
        var Tab = document.getElementById("tab");
        
 
        var row = null;
        var cnt = 0;
        
        
        var ym = year + "년" + (month)+"월";
    
        ymda.innerHTML = ym;
 
        while(tab.rows.length >2){     //테이블의 행의 길이가 2보다 크면 테이블의 행 제거함.
            tab.deleteRow(tab.rows.length -1);
        }
 
        row = Tab.insertRow();
        
 
        for(var j = 0; j<start.getDay(); j++){ //달력의 시작 일 구함
            cell = row.insertCell();
            cnt+=1;
        }
 
        for(var i = 0; i< dayy(year, month); i++){ //달력 일수만큼 찍어줌
        cell = row.insertCell();
        cell.innerHTML = i+1;
        cnt += 1;
 
        if(cnt%7 ==0){ //cnt가 7이면 행을 늘려줌
            row = tab.insertRow();
            
        }
    }
 
    }
    </script>
</head>
<center>
<body onload="changeColor()" id="body">
    
    <table class = "blueText" border="1" id = "tab">
        <tr>
            <td align="center" id="prev" style="width: 23px;"><label onclick="prevmonth()">≤</td>
            <td colspan="5" align="center" id="Ymd" style="width: 150px; height: 30px;"></td>
            <td align="center" id="next" style="width: 23px;"><label onclick="nextmonth()">≥</td>
        </tr>
        
            
        <tr align="center">
            <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 type="text/javascript">
            present();
            
        </script>
 
</body>
</center>
</html>
cs



(실행 결과)


반응형