-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
71 lines (61 loc) · 2.14 KB
/
Copy pathapp.js
File metadata and controls
71 lines (61 loc) · 2.14 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
// Get today's date
const today = new Date();
let currentMonth = today.getMonth();
let currentYear = today.getFullYear();
// Function to generate the calendar
function generateCalendar(month, year) {
const firstDay = new Date(year, month, 1);
const lastDay = new Date(year, month + 1, 0);
const daysInMonth = lastDay.getDate();
const startDay = firstDay.getDay(); // Day of the week the month starts on
// Render the month and year header
const monthName = firstDay.toLocaleString("default", { month: "long" });
document.querySelector(".month h1").textContent = `${monthName} ${year}`;
document.querySelector(".month p").textContent = today.toLocaleDateString(
"default",
{ weekday: "long" }
);
// Clear previous dates
const daysContainer = document.querySelector(".days");
daysContainer.innerHTML = "";
// Generate empty cells for days before the start of the month
for (let i = 0; i < startDay; i++) {
const emptyCell = document.createElement("div");
daysContainer.appendChild(emptyCell);
}
// Create the day cells
for (let day = 1; day <= daysInMonth; day++) {
const dayElement = document.createElement("div");
dayElement.textContent = day;
dayElement.classList.add("day");
// If this is today's date, add the "today" class
if (
day === today.getDate() &&
month === today.getMonth() &&
year === today.getFullYear()
) {
dayElement.classList.add("today");
}
daysContainer.appendChild(dayElement);
}
}
// Event listener for the "previous" button
document.querySelector(".prev").addEventListener("click", () => {
currentMonth--;
if (currentMonth < 0) {
currentMonth = 11;
currentYear--;
}
generateCalendar(currentMonth, currentYear);
});
// Event listener for the "next" button
document.querySelector(".next").addEventListener("click", () => {
currentMonth++;
if (currentMonth > 11) {
currentMonth = 0;
currentYear++;
}
generateCalendar(currentMonth, currentYear);
});
// Initialize the calendar with the current month and year
generateCalendar(currentMonth, currentYear);