Home >Web Front-end >CSS Tutorial >Build a Month Calendar Website

Build a Month Calendar Website

王林
王林Original
2024-08-27 18:01:18594browse

Build a Month Calendar Website

Introduction

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.

Project Overview

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.

Features

  • Dynamic Month Display: Automatically displays the current month and year.
  • Highlighted Current Date: Today’s date is highlighted for easy identification.
  • Accurate Day Layout: Days of the week are correctly aligned according to the month.

Technologies Used

  • HTML: Provides the structure for the Month Calendar.
  • CSS: Styles the calendar, ensuring it is visually appealing and responsive.
  • JavaScript: Handles the logic for generating the calendar, including calculating the days and highlighting the current date.

Project Structure

Here's an overview of the project structure:

Month-Calendar/
├── index.html
├── style.css
└── script.js
  • index.html: Contains the HTML structure for the Month Calendar.
  • style.css: Includes CSS styles to create a modern and responsive design.
  • script.js: Manages the calendar generation functionality.

Installation

To get started with the project, follow these steps:

  1. Clone the repository:

    git clone https://github.com/abhishekgurjar-in/Month-Calendar.git
    
  2. Open the project directory:

    cd Month-Calendar
    
  3. Run the project:

    • Open the index.html file in a web browser to view the Month Calendar.

Usage

  1. Open the website in a web browser.
  2. View the current month with today’s date highlighted.
  3. Navigate through the days to see the layout for the entire month.

Code Explanation

HTML

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:

8b05045a5be5764f313ed5b9168a17e6
49099650ebdc5f3125501fa170048923
  93f0f5c25f18dab9d176bd4f6de5d30e
    7c8d9f814bcad6a1d7abe4eda5f773e5
    acd8feeb3a0ea7477b979779de32785a
    26faf3d1af674280d03ba217d87e9421
    b2386ffb911b14667cb8f0f91ea547a7Month Calendar6e916e0f7d1e588d4f442bf645aedb2f
    af75c476cdb7e6c074ca6da9b40841de
    90392ec4442ad9ff612213ec639da4832cacc6d41bbb37262a98f745aa00fbf0
  9c3bca370b5104690d9ef395f2c5f8d1
  6c04bd5ca3fcae76e30b72ad730ca86d
    4883ec0eb33c31828b7c767c806e14c7
      924ff17625d603f964501dd897c96cc6
        4a249f0d628e2318394fd9b75b4636b1Month Calendar473f0a7621bec819994bb5020d29372a
      16b28748ea4df4d9c2150843fecfba68
      6a0cba8356a55c4a6e47746d431e6421
        418883615a2e79b9926c86666863ce0d
          5f41073e18caf0aa09116baf33d01f47
            4a249f0d628e2318394fd9b75b4636b1473f0a7621bec819994bb5020d29372a
            e388a4556c0f65e1904146cc1a846bee94b3e26ee717c64999d7867364b1b4a3
          16b28748ea4df4d9c2150843fecfba68
        16b28748ea4df4d9c2150843fecfba68
        639769261d891863793f213a2da06288
          dc6dce4a544fdca2df29d5ac0ea9906bMon16b28748ea4df4d9c2150843fecfba68
          dc6dce4a544fdca2df29d5ac0ea9906bTue16b28748ea4df4d9c2150843fecfba68
          dc6dce4a544fdca2df29d5ac0ea9906bWed16b28748ea4df4d9c2150843fecfba68
          dc6dce4a544fdca2df29d5ac0ea9906bThu16b28748ea4df4d9c2150843fecfba68
          dc6dce4a544fdca2df29d5ac0ea9906bFri16b28748ea4df4d9c2150843fecfba68
          dc6dce4a544fdca2df29d5ac0ea9906bSat16b28748ea4df4d9c2150843fecfba68
          dc6dce4a544fdca2df29d5ac0ea9906bSun16b28748ea4df4d9c2150843fecfba68
        16b28748ea4df4d9c2150843fecfba68
        97a12517d2546b2575274c1d8a6e171e16b28748ea4df4d9c2150843fecfba68
      16b28748ea4df4d9c2150843fecfba68
      ffd6ba4147bda351239915f463e46e38
        e388a4556c0f65e1904146cc1a846beeMade with ❤️ by Abhishek Gurjar94b3e26ee717c64999d7867364b1b4a3
      16b28748ea4df4d9c2150843fecfba68
    16b28748ea4df4d9c2150843fecfba68
  36cc49f0c466276486e50c850b7e4956
73a6ac4ed44ffec12cee46588e518a5e

CSS

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;
}

JavaScript

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 += `edfd5fc8d5ee204ac4dfe03b945f3c3916b28748ea4df4d9c2150843fecfba68`;
}
for (let i = 1; i 67a68beefb344083e30784f1b3a9b000${i}16b28748ea4df4d9c2150843fecfba68`;
  } else {
    days += `dc6dce4a544fdca2df29d5ac0ea9906b${i}16b28748ea4df4d9c2150843fecfba68`;
  }
}

daysEl.innerHTML = days;

Live Demo

You can check out the live demo of the Month Calendar project here.

Conclusion

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!

Credits

This project was developed as part of my journey to enhance my JavaScript skills and create functional web tools.

Author

  • Abhishek Gurjar
    • GitHub Profile

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn