当前位置:首页 > JavaScript

js实现日历日程代码

2026-03-01 17:28:35JavaScript

以下是一个基于JavaScript实现的日历日程功能代码示例,包含基础日历渲染和日程管理功能:

日历渲染实现

class Calendar {
  constructor(year, month) {
    this.year = year || new Date().getFullYear();
    this.month = month || new Date().getMonth();
    this.schedules = {};
  }

  render() {
    const firstDay = new Date(this.year, this.month, 1);
    const lastDay = new Date(this.year, this.month + 1, 0);
    const daysInMonth = lastDay.getDate();
    const startDay = firstDay.getDay();

    let html = `<div class="calendar-header">${this.year}年${this.month + 1}月</div>`;
    html += '<div class="calendar-grid">';

    // 星期标题
    const weekdays = ['日', '一', '二', '三', '四', '五', '六'];
    weekdays.forEach(day => {
      html += `<div class="weekday">${day}</div>`;
    });

    // 填充空白格
    for (let i = 0; i < startDay; i++) {
      html += '<div class="day empty"></div>';
    }

    // 填充日期格
    for (let day = 1; day <= daysInMonth; day++) {
      const dateKey = `${this.year}-${this.month + 1}-${day}`;
      const schedules = this.schedules[dateKey] || [];
      html += `<div class="day" data-date="${dateKey}">${day}
        ${schedules.length > 0 ? `<span class="schedule-badge">${schedules.length}</span>` : ''}
      </div>`;
    }

    html += '</div>';
    return html;
  }
}

日程管理功能

class ScheduleManager {
  constructor(calendar) {
    this.calendar = calendar;
  }

  addSchedule(date, title, description) {
    if (!this.calendar.schedules[date]) {
      this.calendar.schedules[date] = [];
    }
    this.calendar.schedules[date].push({
      title,
      description,
      id: Date.now()
    });
  }

  removeSchedule(date, id) {
    if (this.calendar.schedules[date]) {
      this.calendar.schedules[date] = this.calendar.schedules[date].filter(
        item => item.id !== id
      );
    }
  }

  getSchedules(date) {
    return this.calendar.schedules[date] || [];
  }
}

完整示例使用

// 初始化日历
const calendar = new Calendar(2023, 5); // 2023年6月
const scheduleManager = new ScheduleManager(calendar);

// 添加日程
scheduleManager.addSchedule('2023-6-15', '会议', '项目进度讨论');
scheduleManager.addSchedule('2023-6-20', '生日', '朋友生日聚会');

// 渲染日历
document.getElementById('calendar').innerHTML = calendar.render();

// 点击日期显示日程
document.querySelectorAll('.day').forEach(day => {
  day.addEventListener('click', function() {
    const date = this.getAttribute('data-date');
    const schedules = scheduleManager.getSchedules(date);

    if (schedules.length > 0) {
      let scheduleHtml = schedules.map(s => `
        <div class="schedule-item">
          <h4>${s.title}</h4>
          <p>${s.description}</p>
          <button onclick="removeSchedule('${date}', ${s.id})">删除</button>
        </div>
      `).join('');
      document.getElementById('schedule-detail').innerHTML = scheduleHtml;
    }
  });
});

// 全局删除函数
window.removeSchedule = function(date, id) {
  scheduleManager.removeSchedule(date, id);
  document.getElementById('calendar').innerHTML = calendar.render();
};

配套CSS样式

.calendar-header {
  text-align: center;
  font-size: 1.5em;
  padding: 10px;
  background: #f0f0f0;
}

.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 5px;
}

.weekday, .day {
  padding: 10px;
  text-align: center;
  border: 1px solid #ddd;
}

.day {
  cursor: pointer;
  position: relative;
  min-height: 60px;
}

.day:hover {
  background: #f5f5f5;
}

.schedule-badge {
  position: absolute;
  top: 2px;
  right: 2px;
  background: red;
  color: white;
  border-radius: 50%;
  width: 18px;
  height: 18px;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.schedule-item {
  margin: 10px 0;
  padding: 10px;
  border: 1px solid #eee;
  border-radius: 4px;
}

功能扩展建议

  • 添加本地存储功能使用localStorage保存日程
  • 实现月份切换按钮
  • 添加日程提醒功能
  • 支持拖拽调整日程日期
  • 添加日程分类和颜色标记

这个实现提供了基础日历展示和日程管理功能,可根据实际需求进一步扩展和完善。

js实现日历日程代码

标签: 日程日历
分享给朋友:

相关文章

vue日历拖拽实现

vue日历拖拽实现

Vue 日历拖拽实现方案 使用 vue-draggable 库 安装依赖库: npm install vuedraggable 基础拖拽日历实现代码: <template> &…

用vue实现日历

用vue实现日历

实现基础日历结构 使用Vue构建日历需要先设计基础数据结构。通常需要维护当前年份、月份以及日期数组。可以通过Date对象获取当前月份的天数和起始星期。 data() { return {…

vue实现值班日历

vue实现值班日历

Vue 实现值班日历的方法 使用 FullCalendar 插件 FullCalendar 是一个功能强大的日历插件,支持 Vue 集成。安装依赖: npm install @fullcal…

css日历制作

css日历制作

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

php实现日历周末变红

php实现日历周末变红

实现日历周末变红的方法 使用PHP实现日历中周末日期显示为红色,可以通过判断日期是否为周六或周日,然后应用CSS样式来实现。以下是具体步骤: 获取当前月份的天数和第一天是星期几 $year…

js日历案例实现

js日历案例实现

实现基础日历功能 日历功能可以通过JavaScript结合HTML和CSS实现,核心是动态生成日期表格。以下代码展示了一个基础的日历生成器: function generateCalendar(y…