-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
66 lines (58 loc) · 1.85 KB
/
Copy pathscript.js
File metadata and controls
66 lines (58 loc) · 1.85 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
const daysTag = document.querySelector('.days'),
currentDate = document.querySelector('.current-date'),
prevNextIcon = document.querySelectorAll('.icons span')
let date = new Date(),
currYear = date.getFullYear(),
currMonth = date.getMonth()
const months = [
'January',
'February',
'March',
'April',
'May',
'June',
'July',
'August',
'September',
'October',
'November',
'December',
]
const renderCalendar = () => {
let firstDayofMonth = new Date(currYear, currMonth, 1).getDay(), // getting first day of month
lastDateofMonth = new Date(currYear, currMonth + 1, 0).getDate(), // getting last date of month
lastDayofMonth = new Date(currYear, currMonth, lastDateofMonth).getDay(), // getting last day of month
lastDateofLastMonth = new Date(currYear, currMonth, 0).getDate() // getting last date of previous month
let 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 =
i === date.getDate() &&
currMonth === new Date().getMonth() &&
currYear === new Date().getFullYear()
? 'active'
: ''
liTag += `<li class="${isToday}">${i}</li>`
}
for (let i = lastDayofMonth; i < 6; i++) {
liTag += `<li class="inactive">${i - lastDayofMonth + 1}</li>`
}
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, new Date().getDate())
currYear = date.getFullYear()
currMonth = date.getMonth()
} else {
date = new Date()
}
renderCalendar()
})
})