-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrenderer.js
79 lines (72 loc) · 2.04 KB
/
renderer.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
77
78
79
let months = [
"Jan",
"Feb",
"Mar",
"Apr",
"May",
"Jun",
"Jul",
"Aug",
"Sep",
"Oct",
"Nov",
"Dec",
]
const today = new Date()
let currentMonth = today.getMonth()
let currentYear = today.getFullYear()
const selectMonth = document.getElementById("month")
const monthAndYear = document.getElementById("monthAndYear")
const tbl = document.getElementById("calendar-body")
function showCalendar(month, year) {
let firstDay = new Date(year, month).getDay()
let daysInMonth = 32 - new Date(year, month, 32).getDate()
tbl.innerHTML = ""
monthAndYear.innerHTML = months[month] + " " + year
selectMonth.value = month
let date = 1
for (let i = 0; i < 6; i++) {
let row = document.createElement("tr")
for (let j = 0; j < 7; j++) {
if (i === 0 && j < firstDay) {
let cell = document.createElement("td")
let cellText = document.createTextNode("")
cell.appendChild(cellText)
row.appendChild(cell)
} else if (date > daysInMonth) {
let cell = document.createElement("td")
row.appendChild(cell)
} else {
let cell = document.createElement("td")
let cellText = document.createTextNode(date)
cell.style = "background-color: lightblue"
if (
date === today.getDate() &&
year === today.getFullYear() &&
month === today.getMonth()
) {
cell.style = "background-color: red"
}
cell.appendChild(cellText)
row.appendChild(cell)
date++
}
}
tbl.appendChild(row)
}
}
function next() {
currentYear = currentMonth === 11 ? currentYear + 1 : currentYear
currentMonth = (currentMonth + 1) % 12
showCalendar(currentMonth, currentYear)
}
function prev() {
currentYear = currentMonth === 0 ? currentYear - 1 : currentYear
currentMonth = currentMonth === 0 ? 11 : currentMonth - 1
showCalendar(currentMonth, currentYear)
}
function jumpToSelectedMounth() {
currentMonth = +selectMonth.value
showCalendar(currentMonth, currentYear)
}
showCalendar(currentMonth, currentYear)