Home >Web Front-end >Front-end Q&A >How to make a full-year calendar with JavaScript
JavaScript is a very popular programming language that can be used to create a variety of applications and websites. One of the most useful applications is to create a full-year calendar. The full-year calendar is a calendar tool that can display all dates throughout the year. It is very suitable as a convenient time management and recording tool, and can also help people better plan and arrange future schedules.
In this article, we will introduce how to make a full-year calendar in JavaScript, allowing you to learn and practice JavaScript programming skills.
Step One: HTML Skeleton
To make a full-year calendar, you first need to create a container in the HTML page to place the entire calendar. Below is our HTML skeleton:
<!DOCTYPE html> <html> <head> <title>全年日历</title> </head> <body> <div class="calendar"></div> </body> </html>
In this skeleton, we have created a DIV element called "calendar" which will be used to hold our calendar.
Step Two: CSS Styles
In order to make our calendar look beautiful and easy to read, we need to add CSS styles. Here is our CSS code:
.calendar { width: 800px; height: 600px; margin: 0 auto; border: 1px solid #ccc; } .calendar h1 { text-align: center; } .calendar table { width: 100%; height: 100%; border-collapse: collapse; border: 1px solid #ccc; } .calendar table th { background-color: #f2f2f2; text-align: center; font-size: 18px; font-weight: bold; border: 1px solid #ccc; } .calendar table td { text-align: center; font-size: 16px; border: 1px solid #ccc; } .calendar table td.today { background-color: #f2f2f2; }
In the above code, we set some basic styles for the container and set the table cell border to a 1 pixel solid gray line. We also set some colors and font sizes for the title, header, and table data cells.
Step Three: JavaScript Code
Now, we will use JavaScript to dynamically generate our full-year calendar. We'll use Date objects to get and calculate dates, and DOM operations to create and update our HTML elements. Below is the complete JavaScript code:
const calendar = document.querySelector('.calendar'); const months = [ "一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月" ]; // 获取当前日期 const today = new Date(); const currentMonth = today.getMonth(); const currentYear = today.getFullYear(); // 创建并更新标题 const title = document.createElement('h1'); title.textContent = `${currentYear}年`; calendar.appendChild(title); // 创建表格 const table = document.createElement('table'); calendar.appendChild(table); // 创建表头 const thead = document.createElement('thead'); table.appendChild(thead); const weekdayNames = ["日", "一", "二", "三", "四", "五", "六"]; const tr = document.createElement('tr'); weekdayNames.forEach(day => { const th = document.createElement('th'); th.textContent = day; tr.appendChild(th); }); thead.appendChild(tr); // 创建并更新表格数据 const tbody = document.createElement('tbody'); table.appendChild(tbody); // 计算日期并创建表格 for (let month = 0; month < 12; month++) { const tr = document.createElement('tr'); tbody.appendChild(tr); for (let weekday = 0; weekday < 7; weekday++) { const td = document.createElement('td'); tr.appendChild(td); // 计算日期 const date = new Date(currentYear, month, 1 + weekday - new Date(currentYear, month, 1).getDay()); // 添加日期 if (date.getMonth() === month) { td.textContent = date.getDate(); } // 添加类名 if (today.toDateString() === date.toDateString()) { td.classList.add('today'); } // 添加月份标题 if (weekday === 0) { const th = document.createElement('th'); th.setAttribute('colspan', 7); th.textContent = months[month]; tbody.appendChild(th); } } }
In the above code, we first get the current date and store the current month and year. We then used DOM manipulation to create a title, a table, and a header. Next, we use a nested loop to calculate and create the table for each month. We first create rows in the table and then cells within each row. We use a Date object to calculate the date in each cell and add it to the cell. We also added a class name for the current date so that we can change its style using CSS. Finally, we add a header to the first column of each month to show the current month.
Step 4: Test
Let’s now test our code in the browser. Open your browser and enter the URL of the HTML file in the address bar, and you should see a nice full-year calendar with all the months from the current month to the next year.
Conclusion
This article shows how to dynamically create a full-year calendar using JavaScript. We use DOM operations, Date objects and CSS to achieve this. Learning and practicing this project can help you improve your JavaScript programming skills and provide you with a convenient time management tool.
The above is the detailed content of How to make a full-year calendar with JavaScript. For more information, please follow other related articles on the PHP Chinese website!