当前位置:首页 > JavaScript

js实现月历

2026-03-15 11:44:53JavaScript

js实现月历

实现月历的 JavaScript 方法

使用 JavaScript 实现月历需要结合日期处理和 DOM 操作。以下是一个完整的实现方案:

js实现月历

获取当前月份信息

const date = new Date();
const currentYear = date.getFullYear();
const currentMonth = date.getMonth();

生成月历标题

const monthNames = ["一月", "二月", "三月", "四月", "五月", "六月", "七月", "八月", "九月", "十月", "十一月", "十二月"];
document.getElementById('month-title').textContent = `${currentYear}年 ${monthNames[currentMonth]}`;

计算月份天数

function getDaysInMonth(year, month) {
    return new Date(year, month + 1, 0).getDate();
}
const daysInMonth = getDaysInMonth(currentYear, currentMonth);

获取月份第一天是星期几

const firstDay = new Date(currentYear, currentMonth, 1).getDay();

生成月历表格

const calendarBody = document.getElementById('calendar-body');
let dateCell = 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) {
            // 上个月的最后几天
            cell.textContent = '';
        } else if (dateCell > daysInMonth) {
            // 下个月的前几天
            cell.textContent = '';
        } else {
            cell.textContent = dateCell;
            if (dateCell === new Date().getDate() && currentMonth === new Date().getMonth()) {
                cell.classList.add('today');
            }
            dateCell++;
        }

        row.appendChild(cell);
    }

    calendarBody.appendChild(row);
}

完整 HTML 结构

<div class="calendar">
    <h2 id="month-title"></h2>
    <table>
        <thead>
            <tr>
                <th>日</th>
                <th>一</th>
                <th>二</th>
                <th>三</th>
                <th>四</th>
                <th>五</th>
                <th>六</th>
            </tr>
        </thead>
        <tbody id="calendar-body"></tbody>
    </table>
</div>

添加样式

.calendar {
    width: 300px;
    font-family: Arial, sans-serif;
}

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

.calendar th, .calendar td {
    padding: 10px;
    text-align: center;
    border: 1px solid #ddd;
}

.calendar th {
    background-color: #f2f2f2;
}

.calendar .today {
    background-color: #4CAF50;
    color: white;
}

添加月份切换功能

document.getElementById('prev-month').addEventListener('click', () => {
    currentMonth--;
    if (currentMonth < 0) {
        currentMonth = 11;
        currentYear--;
    }
    renderCalendar();
});

document.getElementById('next-month').addEventListener('click', () => {
    currentMonth++;
    if (currentMonth > 11) {
        currentMonth = 0;
        currentYear++;
    }
    renderCalendar();
});

function renderCalendar() {
    // 清空现有日历
    document.getElementById('calendar-body').innerHTML = '';
    document.getElementById('month-title').textContent = `${currentYear}年 ${monthNames[currentMonth]}`;

    // 重新渲染日历
    const daysInMonth = getDaysInMonth(currentYear, currentMonth);
    const firstDay = new Date(currentYear, currentMonth, 1).getDay();

    let dateCell = 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) {
                cell.textContent = '';
            } else if (dateCell > daysInMonth) {
                cell.textContent = '';
            } else {
                cell.textContent = dateCell;
                if (dateCell === new Date().getDate() && currentMonth === new Date().getMonth() && currentYear === new Date().getFullYear()) {
                    cell.classList.add('today');
                }
                dateCell++;
            }

            row.appendChild(cell);
        }

        calendarBody.appendChild(row);
    }
}

这个实现包含了完整的月历功能,包括:

  • 显示当前月份的日历
  • 高亮显示当天日期
  • 支持前后月份切换
  • 响应式布局设计

可以根据需要进一步扩展功能,如添加事件提醒、节假日标记等。

标签: 月历js
分享给朋友:

相关文章

js实现预览

js实现预览

文件上传预览实现 使用JavaScript实现文件上传预览功能,可以通过FileReader对象读取文件内容并显示预览。以下代码支持图片、文本和PDF文件的预览: // HTML部分需要包含inpu…

js实现驼峰

js实现驼峰

实现驼峰命名的几种方法 使用正则表达式和字符串替换 通过正则表达式匹配字符串中的特定模式(如下划线或短横线),并将其后的字母转换为大写,同时移除分隔符。 function toCamelCase(s…

js实现防洪

js实现防洪

防抖(Debounce)实现 防抖的核心思想是在事件触发后延迟执行回调函数,若在延迟时间内再次触发事件,则重新计时。适用于输入框搜索、窗口调整等场景。 function debounce(f…

使用js实现

使用js实现

使用JavaScript实现 JavaScript可以通过Math.random()函数生成随机数,结合其他方法可以实现多种随机数生成需求。 // 生成0到1之间的随机小数 const random…

js实现图

js实现图

JavaScript 实现图的常用方法 在 JavaScript 中,图(Graph)可以通过多种方式实现,常见的包括邻接表(Adjacency List)和邻接矩阵(Adjacency Matrix…

利用js实现

利用js实现

使用 JavaScript 实现 在 JavaScript 中,可以通过多种方式实现功能,具体取决于需求。以下是几种常见的方法: 方法一:使用原生 JavaScript // 示例代码…