Newer
Older
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
import React, { useState } from "react"
import MakeDay from "./MakeDay";
import MakeHeader from "./MakeHeader";
function MakeCell(props){//주차 데이터를 담을 공간
const calendarArr = [];
const startDay = new Date(props.nowYear, props.nowMonth - 1, 1);
const lastDay = new Date(props.nowYear, props.nowMonth, 0);
const lastMonthDate = new Date(props.nowYear, props.nowMonth - 1, 0).getDate();
let nowDate = startDay.getDate();
const lastDate = lastDay.getDate();
// cellb는 이전 달이나 다음 달이라서 색을 연하게 칠하게 할 거고
// cella는 이번 달이라서 진하게 색을 칠할 거임
if(startDay.getDay() > 0){
const tmpArr = [];
for(let i = startDay.getDay(); i > 0; i--)tmpArr.push(
<td className="cellb" onClick={()=>{alert("dd");}}>{lastMonthDate - i + 1}</td>
);
for(let i = startDay.getDay(); i < 7; i++){
tmpArr.push(
<td className="cella" onClick={()=>{alert("dd");}}>{nowDate}</td>
)
nowDate+=1;
}
calendarArr.push(
<tr>{tmpArr}</tr>
);
}
while(nowDate <= lastDate){
const tmpArr = [];
for(let i = 0; i < 7; i++){
let tmp = nowDate;
let cn = "cella";
if(tmp > lastDate){
cn = "cellb";
tmp-=lastDate;
}
tmpArr.push(
<td className={cn} onClick={()=>{alert("dd");}}>{tmp}</td>
)
nowDate+=1;
}
calendarArr.push(
<tr>{tmpArr}</tr>
);
}
return(
<tbody>
{calendarArr}
</tbody>
);
}
function CalendarMonth(){
const [currentDay, setCurrentDay] = useState(new Date());
// 일요일 0 시작
const nowDay = currentDay.getDay();
const [nowDate, setNowDate] = useState(currentDay.getDate());
const [nowMonth, setNowMonth] = useState(currentDay.getMonth() + 1); // zero-base
const [nowYear, setNowYear] = useState(currentDay.getFullYear());
const prevMonth = () => {
let newMonth = nowMonth - 1;
if(newMonth < 1){
newMonth = nowMonth + 11;
setNowYear(nowYear - 1);
}
setNowMonth(newMonth);
setCurrentDay(new Date(nowYear, nowMonth, nowDate));
}
const nextMonth = () => {
let newMonth = nowMonth + 1;
if(newMonth > 12){
newMonth = nowMonth - 11;
setNowYear(nowYear + 1);
}
setNowMonth(newMonth);
setCurrentDay(new Date(nowYear, nowMonth, nowDate));
}
return(
<div className="calendar">
<MakeHeader nowMonth={nowMonth} prevMonth={prevMonth} nextMonth={nextMonth}></MakeHeader>
<h1>{nowYear}</h1>
<table className="calendarTable">
<MakeDay/>
<MakeCell nowYear={nowYear} nowMonth={nowMonth}></MakeCell>
</table>
</div>
);
}
export default CalendarMonth;