Hello, developers! I'm thrilled to share my latest project: a Month Calendar. This project is perfect for anyone looking to create a functional and visually appealing calendar in JavaScript. Whether you're interested in adding a calendar feature to a website or just want to enhance your JavaScript skills, this project will be a valuable addition to your portfolio.
The Month Calendar is a web-based application that displays the current month, highlighting today's date and accurately arranging the days of the week. The project showcases how to dynamically generate a calendar using JavaScript, combined with a sleek and responsive interface built with HTML and CSS.
Here's an overview of the project structure:
Month-Calendar/ ├── index.html ├── style.css └── script.js
To get started with the project, follow these steps:
Clone the repository:
git clone https://github.com/abhishekgurjar-in/Month-Calendar.git
Open the project directory:
cd Month-Calendar
Run the project:
The index.html file defines the structure of the Month Calendar, including the month display and the grid layout for the days. Here’s a snippet:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta http-equiv="X-UA-Compatible" content="IE=edge" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Month Calendar</title> <link rel="stylesheet" href="style.css" /> <script src="script.js" defer></script> </head> <body> <div class="container"> <div class="header"> <h1>Month Calendar</h1> </div> <div class="calendar"> <div class="month"> <div class="date"> <h1></h1> <p></p> </div> </div> <div class="weekdays"> <div>Mon</div> <div>Tue</div> <div>Wed</div> <div>Thu</div> <div>Fri</div> <div>Sat</div> <div>Sun</div> </div> <div class="days"></div> </div> <div class="footer"> <p>Made with ❤️ by Abhishek Gurjar</p> </div> </div> </body> </html>
The style.css file styles the Month Calendar, making it both attractive and responsive. Below are some key styles:
* { margin: 0; padding: 0; font-family: sans-serif; box-sizing: border-box; } .container { width: 100%; height: 100vh; background-color: salmon; display: flex; justify-content: center; align-items: center; flex-direction: column; } .header { margin: 20px; color: white; text-align: center; } .calendar { background-color: black; color: lightgray; width: 450px; height: 520px; border-radius: 10px; box-shadow: 4px 4px 8px rgba(0, 0, 0, 0.4); } .month { width: 100%; height: 120px; background-color: lightseagreen; display: flex; justify-content: center; align-items: center; text-align: center; border-radius: 10px 10px 0 0; } .month h1 { font-size: 30px; font-weight: 400; text-transform: uppercase; } .month p { font-size: 16px; } .weekdays { width: 100%; height: 50px; display: flex; } .weekdays div { font-size: 15px; font-weight: bold; letter-spacing: 1px; width: 100%; display: flex; align-items: center; justify-content: center; } .days { width: 100%; display: flex; flex-wrap: wrap; padding: 2px; } .days div { font-size: 14px; margin: 3px; width: 57.5px; height: 50px; display: flex; justify-content: center; align-items: center; } .days div:hover:not(.empty) { border: 2px solid gray; cursor: pointer; } .today { background-color: lightseagreen; } .footer { margin-top: 70px; color: white; text-align: center; }
The script.js file contains the logic for generating the calendar and highlighting the current date. Here's a snippet:
const monthEl = document.querySelector(".date h1"); const fullDateEl = document.querySelector(".date p"); const daysEl = document.querySelector(".days"); const monthInx = new Date().getMonth(); const lastDay = new Date(new Date().getFullYear(), monthInx + 1, 0).getDate(); const firstDay = new Date(new Date().getFullYear(), monthInx, 1).getDay() - 1; const months = [ "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December", ]; monthEl.innerText = months[monthInx]; fullDateEl.innerText = new Date().toDateString(); let days = ""; for (let i = firstDay; i > 0; i--) { days += `<div class="empty"></div>`; } for (let i = 1; i <= lastDay; i++) { if (i === new Date().getDate()) { days += `<div class="today">${i}</div>`; } else { days += `<div>${i}</div>`; } } daysEl.innerHTML = days;
You can check out the live demo of the Month Calendar project here.
Creating the Month Calendar was an enjoyable project that allowed me to combine my front-end development skills with practical JavaScript functionality. This tool is useful for displaying calendar dates on websites and can be a great addition to your web development projects. I hope you find it as helpful as I do. Happy coding!
This project was developed as part of my journey to enhance my JavaScript skills and create functional web tools.
The above is the detailed content of Build a Month Calendar Website. For more information, please follow other related articles on the PHP Chinese website!