-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<!DOCTYPE html> | ||
<html lang="en"> | ||
<head> | ||
<meta charset="UTF-8" /> | ||
<meta name="viewport" content="width=device-width, initial-scale=1.0" /> | ||
<title>Simple Calendar</title> | ||
<script src="https://cdn.tailwindcss.com"></script> | ||
<script type="module"> | ||
import { | ||
format, | ||
subMonths, | ||
addMonths, | ||
} from "https://cdn.jsdelivr.net/npm/[email protected]/esm/index.js"; | ||
|
||
document.addEventListener("DOMContentLoaded", function () { | ||
const calendarTitle = document.getElementById("calendar-title"); | ||
const daysContainer = document.getElementById("days-container"); | ||
let currentDate = new Date(); | ||
|
||
function renderCalendar(date) { | ||
const startDay = new Date( | ||
date.getFullYear(), | ||
date.getMonth(), | ||
1 | ||
).getDay(); | ||
const daysInMonth = new Date( | ||
date.getFullYear(), | ||
date.getMonth() + 1, | ||
0 | ||
).getDate(); | ||
calendarTitle.textContent = format(date, "MMMM yyyy"); | ||
daysContainer.innerHTML = ""; | ||
|
||
for (let i = 0; i < startDay; i++) { | ||
daysContainer.innerHTML += | ||
'<div class="border border-gray-300 w-14 h-14 flex items-center justify-center"></div>'; | ||
} | ||
|
||
for (let i = 1; i <= daysInMonth; i++) { | ||
daysContainer.innerHTML += `<div class="border border-gray-300 w-14 h-14 flex items-center justify-center">${i}</div>`; | ||
} | ||
} | ||
|
||
document | ||
.getElementById("prev-month") | ||
.addEventListener("click", function () { | ||
currentDate = subMonths(currentDate, 1); | ||
renderCalendar(currentDate); | ||
}); | ||
|
||
document | ||
.getElementById("next-month") | ||
.addEventListener("click", function () { | ||
currentDate = addMonths(currentDate, 1); | ||
renderCalendar(currentDate); | ||
}); | ||
|
||
renderCalendar(currentDate); | ||
}); | ||
</script> | ||
<style> | ||
/* Additional styles can be placed here */ | ||
</style> | ||
</head> | ||
<body class="bg-gray-100 p-8"> | ||
<div class="bg-white p-4 rounded shadow-md"> | ||
<div class="flex items-center justify-between mb-4"> | ||
<button | ||
id="prev-month" | ||
class="p-2 bg-gray-200 rounded hover:bg-gray-300" | ||
> | ||
< | ||
</button> | ||
<h2 id="calendar-title" class="text-xl font-semibold"></h2> | ||
<button | ||
id="next-month" | ||
class="p-2 bg-gray-200 rounded hover:bg-gray-300" | ||
> | ||
> | ||
</button> | ||
</div> | ||
<div class="grid grid-cols-7 gap-2"> | ||
<div class="font-bold text-center">Sun</div> | ||
<div class="font-bold text-center">Mon</div> | ||
<div class="font-bold text-center">Tue</div> | ||
<div class="font-bold text-center">Wed</div> | ||
<div class="font-bold text-center">Thu</div> | ||
<div class="font-bold text-center">Fri</div> | ||
<div class="font-bold text-center">Sat</div> | ||
</div> | ||
<div id="days-container" class="grid grid-cols-7 gap-2 mt-2"> | ||
<!-- Calendar days will be rendered here --> | ||
</div> | ||
</div> | ||
</body> | ||
</html> |