forked from peter001019/WebProgrammingProject
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalendar.js
More file actions
94 lines (78 loc) · 4.37 KB
/
Copy pathcalendar.js
File metadata and controls
94 lines (78 loc) · 4.37 KB
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
window.onload = function () { buildCalendar(); } // 웹 페이지가 로드되면 buildCalendar 실행
let nowMonth = new Date(); // 현재 달을 페이지를 로드한 날의 달로 초기화
let today = new Date(); // 페이지를 로드한 날짜를 저장
today.setHours(0,0,0,0); // 비교 편의를 위해 today의 시간을 초기화
//선택한 날짜를 저장하는 input element: dateInput
let dateInput = document.createElement('input');
dateInput.type = 'hidden';
dateInput.name = 'selectedDate';
// 달력 생성 : 해당 달에 맞춰 테이블을 만들고, 날짜를 채워 넣는다.
function buildCalendar(){
let firstDate = new Date(nowMonth.getFullYear(), nowMonth.getMonth(), 1); // 이번달 1일
let lastDate = new Date(nowMonth.getFullYear(), nowMonth.getMonth() + 1, 0); // 이번달 마지막날
let tbody_Calendar = document.querySelector(".Calendar > tbody");
document.getElementById("calYear").innerText = nowMonth.getFullYear(); // 연도 숫자 갱신
document.getElementById("calMonth").innerText = leftPad(nowMonth.getMonth() + 1); // 월 숫자 갱신
while (tbody_Calendar.rows.length > 0) { // 이전 출력결과가 남아있는 경우 초기화
tbody_Calendar.deleteRow(tbody_Calendar.rows.length - 1);
}
let nowRow = tbody_Calendar.insertRow(); // tr추가
for (let j = 0; j < firstDate.getDay(); j++) { // 이번달 1일의 요일만큼
let nowColumn = nowRow.insertCell(); // td추가
}
for (let nowDay = firstDate; nowDay <= lastDate; nowDay.setDate(nowDay.getDate() + 1)) { // day는 날짜를 저장하는 변수, 이번달 마지막날까지 증가시키며 반복
let nowColumn = nowRow.insertCell(); // 새 열을 추가하고
nowColumn.innerText = leftPad(nowDay.getDate()); // 추가한 열에 날짜 입력
let choiced_Day = new Date(nowDay); //선택한 날짜를 저장
if (nowDay.getDay() == 0) { // 일요일인 경우 글자색 빨강으로
nowColumn.style.color = "#DC143C";
}
if (nowDay.getDay() == 6) { // 토요일인 경우 글자색 파랑으로 하고
nowColumn.style.color = "#0000CD";
nowRow = tbody_Calendar.insertRow(); // 새로운 행 추가
}
if (nowDay < today) { // 지난날인 경우
nowColumn.className = "pastDay";
}
else if (nowDay.getFullYear() == today.getFullYear() && nowDay.getMonth() == today.getMonth() && nowDay.getDate() == today.getDate()) { // 오늘인 경우
nowColumn.className = "today";
nowColumn.onclick = function () {
choiceDate(this, choiced_Day);
}
}
else { // 미래인 경우
nowColumn.className = "futureDay";
nowColumn.onclick = function () {
choiceDate(this, choiced_Day);
}
}
}
}
// 날짜 선택
function choiceDate(nowColumn, choiced_Day) {
if (document.getElementsByClassName("choiceDay")[0]) { // 기존에 선택한 날짜가 있으면
document.getElementsByClassName("choiceDay")[0].classList.remove("choiceDay"); // 해당 날짜의 "choiceDay" class 제거
}
nowColumn.classList.add("choiceDay"); // 선택된 날짜에 "choiceDay" class 추가
dateInput.value = choiced_Day.getFullYear() + '-' + (choiced_Day.getMonth() + 1) + '-' + choiced_Day.getDate(); //문자열 형태로 날짜를 dateInput.value에 저장
console.log(dateInput.value);
document.querySelector('form').prepend(dateInput);
}
// 이전달 버튼 클릭
function prevCalendar() {
nowMonth = new Date(nowMonth.getFullYear(), nowMonth.getMonth() - 1, nowMonth.getDate()); // 현재 달을 1 감소
buildCalendar(); // 달력 다시 생성
}
// 다음달 버튼 클릭
function nextCalendar() {
nowMonth = new Date(nowMonth.getFullYear(), nowMonth.getMonth() + 1, nowMonth.getDate()); // 현재 달을 1 증가
buildCalendar(); // 달력 다시 생성
}
// input값이 한자리 숫자인 경우 앞에 '0' 붙혀주는 함수
function leftPad(value) {
if (value < 10) {
value = "0" + value;
return value;
}
return value;
}