-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
78 lines (63 loc) · 1.7 KB
/
script.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
const header = document.querySelector(".calendar h3");
const dates = document.querySelector(".dates");
const navs = document.querySelectorAll("#prev, #next");
const months = [
"January",
"February",
"March",
"April",
"May",
"June",
"July",
"August",
"September",
"October",
"November",
"December",
];
let date = new Date();
let month = date.getMonth();
let year = date.getFullYear();
function renderCalendar() {
const start = new Date(year, month, 1).getDay();
const endDate = new Date(year, month + 1, 0).getDate();
const end = new Date(year, month, endDate).getDay();
const endDatePrev = new Date(year, month, 0).getDate();
let datesHtml = "";
for (let i = start; i > 0; i--) {
datesHtml += `<li class="inactive">${endDatePrev - i + 1}</li>`;
}
for (let i = 1; i <= endDate; i++) {
let className =
i === date.getDate() &&
month === new Date().getMonth() &&
year === new Date().getFullYear()
? ' class="today"'
: "";
datesHtml += `<li${className}>${i}</li>`;
}
for (let i = end; i < 6; i++) {
datesHtml += `<li class="inactive">${i - end + 1}</li>`;
}
dates.innerHTML = datesHtml;
header.textContent = `${months[month]} ${year}`;
}
navs.forEach((nav) => {
nav.addEventListener("click", (e) => {
const btnId = e.target.id;
if (btnId === "prev" && month === 0) {
year--;
month = 11;
} else if (btnId === "next" && month === 11) {
year++;
month = 0;
} else {
month = btnId === "next" ? month + 1 : month - 1;
}
date = new Date(year, month, new Date().getDate());
year = date.getFullYear();
month = date.getMonth();
renderCalendar();
});
});
renderCalendar();