-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
56 lines (46 loc) · 1.88 KB
/
app.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
const currentDate = document.querySelector(".current-date"),
daysTag = document.querySelector(".days"),
prevNextIcon = document.querySelectorAll(".icons span");
let date = new Date(),
currYear = date.getFullYear(),
currMonth = date.getMonth();
const months = ["Jan' ", "Feb' ", "Mar' ", "Apr' ", "May", "June", "July", "Aug' ", "Sept' ", "Oct' ", "Nov' ", "Dec' "];
const renderCalendar = () => {
let firstDayOfMonth = new Date(currYear, currMonth, 1).getDay(),
lastDateOfMonth = new Date(currYear, currMonth + 1, 0).getDate(),
lastDayOfMonth = new Date(currYear, currMonth, lastDateOfMonth).getDay(),
lastDateOfLastMonth = new Date(currYear, currMonth, 0).getDate(),
liTag = "";
for (let i = firstDayOfMonth; i > 0; i--) {
liTag += `<li class="inactive">${lastDateOfLastMonth - i + 1}</li>`;
}
for (let i = 1; i <= lastDateOfMonth; i++) {
let isToday;
if(i == date.getDate() && currMonth == new Date().getMonth() && currYear == new Date().getFullYear()) {
isToday = "active";
} else {
isToday = "";
}
liTag += `<li class="${isToday}">${i}</li>`;
}
for (let i = lastDayOfMonth; i < 6; i++) {
liTag += `<li class="inactive">${i - lastDayOfMonth + 1}</li>`;
}
console.log(liTag);
currentDate.innerText = `${months[currMonth]} ${currYear}`;
daysTag.innerHTML = liTag;
}
renderCalendar();
prevNextIcon.forEach(icon => {
icon.addEventListener("click", () => {
currMonth = icon.id === "prev" ? currMonth - 1 : currMonth + 1;
if(currMonth < 0 || currMonth > 11) {
date = new Date(currYear, currMonth);
currYear = date.getFullYear();
currMonth = date.getMonth();
} else {
date = new Date();
}
renderCalendar();
})
})