当前位置:首页 > JavaScript

js实现日历日程代码

2026-03-01 17:28:35JavaScript

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

js实现日历日程代码

日历渲染实现

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保存日程
  • 实现月份切换按钮
  • 添加日程提醒功能
  • 支持拖拽调整日程日期
  • 添加日程分类和颜色标记

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

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

相关文章

vue实现每日签到日历

vue实现每日签到日历

实现每日签到日历的基本思路 使用Vue实现每日签到日历需要结合日期处理和组件化设计。核心功能包括生成当月日历数据、标记签到状态以及交互逻辑。 日历数据生成 通过JavaScript的Date对象处理…

vue实现全年日历功能

vue实现全年日历功能

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

vue实现日历周滑动

vue实现日历周滑动

Vue 实现日历周滑动功能 核心思路 日历周滑动功能通常需要结合日期计算、动态渲染和触摸事件处理。Vue 的响应式特性可以轻松实现数据与视图的同步更新。 日期计算逻辑 使用 Date 对象或第三方库…

js日历实现

js日历实现

实现日历的基本思路 日历的核心功能是展示日期,并允许用户选择日期。需要处理年、月、日的计算,以及界面布局。 获取当前日期 使用 Date 对象获取当前日期信息: const date = new…

uniapp仿钉钉日历

uniapp仿钉钉日历

uniapp仿钉钉日历实现方法 日历组件选择 使用uni-app的uni-calendar组件作为基础,钉钉日历的核心功能包括月视图切换、日程标记和点击事件。需自定义样式和功能扩展以满足仿钉钉需求。…

css制作日历

css制作日历

使用CSS Grid布局制作日历 日历的布局适合使用CSS Grid实现,可以轻松定义行和列的结构。以下是一个基础的日历HTML结构和CSS样式: <div class="calendar"&…