calendar
Sat Nov 18 2023 05:56:12 GMT+0000 (Coordinated Universal Time)
Saved by @haha_7002
<!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> <style> body { font-family: Arial, sans-serif; margin: 20px; } table { width: 100%; border-collapse: collapse; margin-top: 20px; } th, td { border: 1px solid #ddd; padding: 8px; text-align: center; } th { background-color: #f2f2f2; } </style> </head> <body> <h2>Simple Calendar</h2> <button onclick="previousMonth()">Previous Month</button> <button onclick="nextMonth()">Next Month</button> <table id="calendar"> <thead> <tr> <th>Sun</th> <th>Mon</th> <th>Tue</th> <th>Wed</th> <th>Thu</th> <th>Fri</th> <th>Sat</th> </tr> </thead> <tbody id="calendar-body"> <!-- Calendar content will go here --> </tbody> </table> <script> function generateCalendar(year, month) { const calendarBody = document.getElementById('calendar-body'); calendarBody.innerHTML = ''; const firstDay = new Date(year, month, 1); const lastDay = new Date(year, month + 1, 0); const daysInMonth = lastDay.getDate(); 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.getDay()) || date > daysInMonth) { // Empty cells before the first day and after the last day cell.textContent = ''; } else { cell.textContent = date; date++; } row.appendChild(cell); } calendarBody.appendChild(row); } } function updateCalendar() { const currentDate = new Date(); generateCalendar(currentDate.getFullYear(), currentDate.getMonth()); } function previousMonth() { const currentDate = new Date(); const currentMonth = currentDate.getMonth(); currentDate.setMonth(currentMonth - 1); generateCalendar(currentDate.getFullYear(), currentDate.getMonth()); } function nextMonth() { const currentDate = new Date(); const currentMonth = currentDate.getMonth(); currentDate.setMonth(currentMonth + 1); generateCalendar(currentDate.getFullYear(), currentDate.getMonth()); } // Initial calendar generation updateCalendar(); </script> </body> </html>
Comments