forked from thinkswell/javascript-mini-projects
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
84 lines (66 loc) · 2.15 KB
/
scripts.js
File metadata and controls
84 lines (66 loc) · 2.15 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
const today = new Date();
let currentMonth = today.getMonth();
let currentYear = today.getFullYear();
const selectYear = document.getElementById("year");
const selectMonth = document.getElementById("month");
const monthAndYear = document.getElementById("monthAndYear");
const calendarBody = document.getElementById("calendar-body");
const months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun",
"Jul", "Aug", "Sep", "Oct", "Nov", "Dec"];
document.addEventListener("DOMContentLoaded", () => {
showCalendar(currentMonth, currentYear);
});
function next() {
currentMonth = (currentMonth + 1) % 12;
if (currentMonth === 0) currentYear++;
showCalendar(currentMonth, currentYear);
}
function previous() {
currentMonth = (currentMonth === 0) ? 11 : currentMonth - 1;
if (currentMonth === 11) currentYear--;
showCalendar(currentMonth, currentYear);
}
function jump() {
const selectedYear = parseInt(selectYear.value);
const selectedMonth = parseInt(selectMonth.value);
if (!isNaN(selectedYear) && !isNaN(selectedMonth)) {
currentYear = selectedYear;
currentMonth = selectedMonth;
showCalendar(currentMonth, currentYear);
}
}
function showCalendar(month, year) {
const firstDay = new Date(year, month).getDay();
const daysInMonth = 32 - new Date(year, month, 32).getDate();
// Clear previous calendar
calendarBody.innerHTML = "";
// Set month and year
monthAndYear.textContent = `${months[month]} ${year}`;
selectYear.value = year;
selectMonth.value = month;
let date = 1;
for (let i = 0; i < 6; i++) {
const row = document.createElement("tr");
for (let j = 0; j < 7; j++) {
const cell = document.createElement("td");
if (i === 0 && j < firstDay) {
cell.textContent = "";
} else if (date > daysInMonth) {
break;
} else {
cell.textContent = date;
// Highlight today
if (
date === today.getDate() &&
year === today.getFullYear() &&
month === today.getMonth()
) {
cell.classList.add("bg-info");
}
date++;
}
row.appendChild(cell);
}
calendarBody.appendChild(row);
}
}