[Moment.js]동적 달력(캘린더)만들기

[Moment.js]동적 달력(캘린더)만들기

JS로 달에 맞게 생성되는 동적 달력을 그려보자.

html 태그로만 달력그리기

html코드로만 달력을 그린다면 아래와 같은 것이다.
하지만 이렇게 그린다면 달마다 바뀌는 달력이 아닌 정적달력이 되버린다.
매달 달라지는 달력은 어떻게 만들까?

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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
<div>
<div>
<a><i class="fa fa-arrow-left" aria-hidden="true"></i>이전달</a>
<p id="title-date">----년 -월</p>
<a><i class="fa fa-arrow-right" aria-hidden="true"></i>다음달</a>
</div>
<div>
<table id="month-table" class="table_cal">
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
<tbody>
<tr>
<td>
<p></p>
<span></span>
</td>
<td>
<p></p>
<span></span>
</td>
<td>
<p></p>
<span></span>
</td>
<td>
<p></p>
<span></span>
</td>
<td>
<p></p>
<span></span>
</td>
<td>
<p>1</p>
<span></span>
</td>
<td>
<p>2</p>
<span></span>
</td>
</tr>
<tr>
<td>
<p>3</p>
<span>

</span>
</td>
<td>
<p>4</p>
<span>

</span>
</td>
<td>
<p>5</p>
<span>
<a href="javascript:void(0);"></a>
</span>
</td>
<td>
<p>6</p>
<span>
</span>
</td>
<td>
<p>7</p>
<span>
</span>
</td>
<td >
<p>8</p>
<span>
</span>
</td>
<td>
<p>9</p>
<span>

</span>
</td>
</tr>
<tr>
<td>
<p>10</p>
<span>

</span>
</td>
<td>
<p>11</p>
<span>

</span>
</td>
<td>
<p>12</p>
<span>

</span>
</td>
<td>
<p>13</p>
<span>

</span>
</td>
<td>
<p>14</p>
<span>

</span>
</td>
<td class="possible">
<p>15</p>
<span>
<a href="javascript:void(0);"></a>
</span>
</td>
<td>
<p>16</p>
<span>

</span>
</td>
</tr>
<tr>
<td>
<p>17</p>
<span>

</span>
</td>
<td>
<p>18</p>
<span>

</span>
</td>
<td>
<p>19</p>
<span>

</span>
</td>
<td>
<p>20</p>
<span>

</span>
</td>
<td>
<p>21</p>
<span>

</span>
</td>
<td class="possible">
<p>22</p>
<span>
<a href="javascript:void(0);"></a>
</span>
</td>
<td>
<p>23</p>
<span>

</span>
</td>
</tr>
<tr>
<td>
<p>24</p>
<span>

</span>
</td>
<td>
<p>25</p>
<span>

</span>
</td>
<td>
<p>26</p>
<span>

</span>
</td>
<td>
<p>27</p>
<span>
<a href="javascript:void(0);"></a>
</span>
</td>
<td>
<p>28</p>
<span>

</span>
</td>
<td>
<p>29</p>
<span>
<a href="javascript:void(0);"></a>
</span>
</td>
<td>
<p>30</p>
<span></span>
</td>
</tr>
<tr>
<td>
<p>31</p>
<span></span>
</td>
<td>
<p></p>
<span></span>
</td>
<td>
<p></p>
<span></span>
</td>
<td>
<p></p>
<span></span>
</td>
<td>
<p></p>
<span></span>
</td>
<td>
<p></p>
<span></span>
</td>
<td>
<p></p>
<span></span>
</td>
</tr>
</tbody>
</table>
</div>
</div>




동적 달력

위 html코드에서 thead와 tbody부분을 제거했다.
restDaysWeekNumber를 통해서 주말과 평일을 나눠서 달력에 특별한 표시를 할수도 있다.
해당 코드를 사용하기 위해서는 moment.js가 있어야한다.

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
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js" integrity="sha512-CryKbMe7sjSCDPl18jtJI5DR5jtkUWxPXWaLCst6QjH8wxDexfRJic2WRmRXmstr2Y8SxDDWuBO6CQC6IE4KTA==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
<script>
const createMonthTable = async (date) => {
let firstDate = new Date(date.getFullYear(), date.getMonth(), 1);
let lastDate = new moment(firstDate).add(1, 'months').subtract(1, 'days');
let currentDate = firstDate;
$('#title-date').text(moment(firstDate).format("YYYY년 MM월"));
$('#title-date').data("firstdate", firstDate);
let table = $("#month-table");
let calenderTable = $("<table id='month-table'></table>");
calenderTable.empty();

const today = moment();
let tempCurrentDate;
let daysNumber = 0;

let theadTr = $("<thead></thead>");
theadTr.appendTo(calenderTable);

let headTr = $("<tr></tr>");
for (let i = 0; i < 7; i++) {
let th = $("<th>" + moment().day(i).format("ddd") + "</th>");
headTr.append(th);
}
headTr.appendTo(theadTr);

let restDaysWeekNumber = "60"; /* 정기휴일(일:0,월:1,화:2, ... ,금:5,토:6) */
while (currentDate <= lastDate) {
tempCurrentDate = new moment(currentDate);
let daysTr = $("<tr></tr>");

for (let i = 0; i < 7; i++) { // 여기서 i는 요일
let td = $("<td></td>");

if (restDaysWeekNumber.includes(i)) {
td.addClass("blocked"); // 주말표시가 된 class를 추가할 수 있음
}

if (tempCurrentDate.day() == i && firstDate <= tempCurrentDate && tempCurrentDate <= lastDate) {
let p = $("<p>"+tempCurrentDate.date()+"</p>");
p.appendTo(td);

let span = $("<span></span>");
span.appendTo(td);

tempCurrentDate.add(1, 'days');
}

td.appendTo(daysTr);
}
daysTr.appendTo(calenderTable);
currentDate = new moment(tempCurrentDate);
}

let parent = table.parent();
table.remove();
calenderTable.appendTo(parent);
};
// 달력 이전달 이동
const makePreCalender = () => {
preDateString = moment($('#title-date').data('firstdate')).add('-1','M').format("YYYY-MM-DD");
createMonthTable(new Date(preDateString));
}
// 달력 다음달 이동
const makeNextCalender = () => {
nextDateString = moment($('#title-date').data('firstdate')).add('1','M').format("YYYY-MM-DD");
createMonthTable(new Date(nextDateString));
}

$(document).ready( () => {

var data = createMonthTable(new Date());

});
</script>

<div>
<div>
<a href="javascript:makePreCalender();"><i class="fa fa-arrow-left" aria-hidden="true"></i>이전달</a>
<p id="title-date">----년 -월</p>
<a href="javascript:makeNextCalender();"><i class="fa fa-arrow-right" aria-hidden="true"></i>다음달</a>
</div>
<div>
<table id="month-table">
<thead>
<tr>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
</tr>
</thead>
</table>
</div>
</div>

완성!

Comments