当前位置:首页 > JavaScript

js日历案例实现

2026-03-15 21:04:30JavaScript

实现基础日历功能

使用JavaScript创建一个简单的日历,需要获取当前日期并生成对应的月份视图。以下是基础实现代码:

function createCalendar(year, month) {
  const daysInMonth = new Date(year, month + 1, 0).getDate();
  const firstDayOfMonth = new Date(year, month, 1).getDay();

  let calendarHTML = '<table><tr><th>日</th><th>一</th><th>二</th><th>三</th><th>四</th><th>五</th><th>六</th></tr><tr>';

  // 填充空白单元格
  for (let i = 0; i < firstDayOfMonth; i++) {
    calendarHTML += '<td></td>';
  }

  // 填充日期
  for (let day = 1; day <= daysInMonth; day++) {
    calendarHTML += `<td>${day}</td>`;

    if ((day + firstDayOfMonth) % 7 === 0) {
      calendarHTML += '</tr><tr>';
    }
  }

  calendarHTML += '</tr></table>';
  document.getElementById('calendar').innerHTML = calendarHTML;
}

// 使用当前年月初始化日历
const currentDate = new Date();
createCalendar(currentDate.getFullYear(), currentDate.getMonth());

添加导航功能

为日历添加上一月和下一月的导航按钮:

let currentYear = new Date().getFullYear();
let currentMonth = new Date().getMonth();

function updateCalendar() {
  document.getElementById('month-year').textContent = 
    `${currentYear}年 ${currentMonth + 1}月`;
  createCalendar(currentYear, currentMonth);
}

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

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

updateCalendar();

高亮当前日期

在日历中突出显示当前日期:

function createCalendar(year, month) {
  const today = new Date();
  const isCurrentMonth = year === today.getFullYear() && month === today.getMonth();

  // ...之前的代码...

  for (let day = 1; day <= daysInMonth; day++) {
    const isToday = isCurrentMonth && day === today.getDate();
    calendarHTML += `<td${isToday ? ' class="today"' : ''}>${day}</td>`;
    // ...之后的代码...
  }
}

添加事件处理

为日期单元格添加点击事件:

function createCalendar(year, month) {
  // ...之前的代码...

  for (let day = 1; day <= daysInMonth; day++) {
    calendarHTML += `<td onclick="selectDate(${year}, ${month}, ${day})">${day}</td>`;
    // ...之后的代码...
  }
}

function selectDate(year, month, day) {
  const selectedDate = new Date(year, month, day);
  console.log('选择的日期:', selectedDate.toLocaleDateString());
}

完整HTML结构

日历的完整HTML结构示例:

<div class="calendar-container">
  <div class="calendar-header">
    <button id="prev-month">上个月</button>
    <h2 id="month-year"></h2>
    <button id="next-month">下个月</button>
  </div>
  <div id="calendar"></div>
</div>

<style>
  table {
    border-collapse: collapse;
    width: 100%;
  }
  th, td {
    border: 1px solid #ddd;
    padding: 8px;
    text-align: center;
  }
  th {
    background-color: #f2f2f2;
  }
  .today {
    background-color: #4CAF50;
    color: white;
  }
  .calendar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 10px;
  }
</style>

使用第三方库

如果需要更复杂的日历功能,可以考虑使用现成的JavaScript库:

  • FullCalendar:功能全面的日历组件
  • Pikaday:轻量级日期选择器
  • Flatpickr:现代化的日期时间选择器

例如使用FullCalendar的简单实现:

js日历案例实现

document.addEventListener('DOMContentLoaded', function() {
  const calendarEl = document.getElementById('calendar');
  const calendar = new FullCalendar.Calendar(calendarEl, {
    initialView: 'dayGridMonth',
    headerToolbar: {
      left: 'prev,next today',
      center: 'title',
      right: 'dayGridMonth,timeGridWeek,timeGridDay'
    }
  });
  calendar.render();
});

标签: 日历案例
分享给朋友:

相关文章

vue实现全年日历功能

vue实现全年日历功能

实现全年日历功能 在Vue中实现全年日历功能,可以通过组合多个月份日历组件或使用第三方库来简化开发。以下是两种常见方法: 使用第三方库(如FullCalendar) 安装FullCalendar V…

vue3实现日历

vue3实现日历

Vue3 日历组件实现方法 基础日历结构 使用Vue3的Composition API可以快速构建日历核心逻辑。以下代码展示如何生成当月日历网格: <script setup> impo…

用vue实现routerview案例

用vue实现routerview案例

使用 Vue Router 实现 RouterView 案例 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是一个完整的实现案例。 安装 Vue Rou…

vue实现天气小案例

vue实现天气小案例

实现天气小案例的步骤 创建Vue项目 使用Vue CLI创建一个新项目,确保安装了Vue和必要的依赖项。可以通过以下命令创建项目: vue create weather-app cd weather…

vue实现点赞案例

vue实现点赞案例

Vue实现点赞功能 基础实现(无持久化) 创建Vue组件,使用v-on绑定点击事件和v-bind动态绑定样式: <template> <button @click=…

css日历制作

css日历制作

CSS日历制作方法 使用HTML和CSS创建一个简单的日历布局,可以通过以下方式实现: HTML结构 <div class="calendar"> <div class="m…