How to make a monthly calendar with JavaScript

王林
Release: 2023-05-16 12:45:08
Original
625 people have browsed it

Introduction to how to make a monthly calendar using JavaScript

JavaScript is a programming language used for web development. It is a dynamic scripting language that is usually used for client-side web development. In this article, we will introduce how to use JavaScript to create a simple monthly calendar on a web page.

Requirements:

Before making the monthly calendar, we need the following files:

1. An HTML file for building a web interface

2.CSS File, used to apply styles to HTML files

3. JavaScript file, used to add the functionality of a monthly calendar

Next, let’s create a monthly calendar from scratch.

Create HTML file

First create an HTML file, we can write it from scratch, or use a template and save it as a .html file. Add the following code to the file:




    
    月历

月历

星期日 星期一 星期二 星期三 星期四 星期五 星期六
Copy after login

In this HTML file, we define an h1 tag titled "Month Calendar" and add an attribute with the id "currentMonth" within the tag. This attribute Will be used to display the current month in JavaScript code. We also use a table tag to display the calendar. There are seven columns in this table, corresponding to the seven-day week. A tbody tag is used to generate the monthly calendar table in JavaScript code, and we also added a script tag that contains our JavaScript file that will be used to generate the monthly calendar for the page.

We can also add some CSS styles to this HTML file to beautify the page:

table {
    border-collapse: collapse;
    width: 100%;
}

th, td {
    border: 1px solid black;
    text-align: center;
}

th {
    height: 25px;
    background-color: #cccccc;
}

td {
    height: 50px;
}
Copy after login

These styles will add some basic styles to the table, th and td elements of the page.

Create JavaScript file

Now, we need to create a JavaScript file to add a monthly calendar function to the page. We save this file as "calendar.js".

In this file, we define a function to create a monthly calendar table:

function createCalendar(month, year) {
    var weekdays = ["日","一","二","三","四","五","六"];
    var calendarBody = document.getElementById("calendarBody");
    var daysInMonth = new Date(year, month+1, 0).getDate();
    var date = new Date(year, month, 1);
    var row = document.createElement("tr");

    for (var i = 0; i < weekdays.length; i++) {
        var cell = document.createElement("th");
        cell.innerText = weekdays[i];
        row.appendChild(cell);
    }

    calendarBody.appendChild(row);

    for (var i = 1; i <= daysInMonth; i++) {
        var newDate = new Date(year, month, i);
        var dayOfWeek = newDate.getDay();
        if (dayOfWeek === 0) {
            row = document.createElement("tr");
            calendarBody.appendChild(row);
        }
        var cell = document.createElement("td");
        cell.innerText = i;
        row.appendChild(cell);
    }
}
Copy after login

In this function, we first define an array to store the names of Sunday to Saturday . We also obtained the tbody element through the document.getElementById method, and obtained the number of days in the current month and the date of the first day. Next, we created a table header row and added a header cell containing the name of the day of the week in this row. Then, we add date cells row by row, and if a date cell encounters a Sunday, we create a new row.

Next, we need to add a function to update the current month of the monthly calendar:

function updateCalendar() {
    var currentMonth = document.getElementById("currentMonth");
    var currentDate = new Date();
    var month = currentDate.getMonth();
    var year = currentDate.getFullYear();
    currentMonth.innerText = year + "年" + (month+1) + "月";
    createCalendar(month, year);
}
Copy after login

In this function, we first use the document.getElementById method to get the h1 element of the current month, and then create a Date object to get the current date, month, year, and set the innerText attribute of the h1 element.

Finally, we need to call the updateCalendar function to generate the monthly calendar:

window.onload = function() {
    updateCalendar();
}
Copy after login

This code will call the updateCalendar function after the page is fully loaded.

At this point, we have completed the production of the monthly calendar. Now we can open this HTML file in the browser and see the generated monthly calendar.

The above is the detailed content of How to make a monthly calendar with JavaScript. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!