当前位置:首页 > JavaScript

js实现日历日程代码

2026-03-01 17:28:35JavaScript

js实现日历日程代码

js实现日历日程代码

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

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

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

相关文章

vue VUE实现日历组件功能

vue VUE实现日历组件功能

Vue 日历组件实现方法 基础日历结构 使用Vue的模板语法构建日历的HTML结构,通常包含头部(年月显示、切换按钮)和日历表格(星期标题和日期格子)。 <template> <…

vue滚动实现日历组件

vue滚动实现日历组件

实现思路 基于Vue实现滚动日历组件的核心在于动态生成日期数据,并通过CSS和触摸事件实现平滑滚动效果。关键在于处理日期计算、渲染优化和交互逻辑。 基础结构设计 <template>…

react实现携程日历

react实现携程日历

实现携程日历的React组件 创建一个类似携程的日历组件需要处理日期选择、区间高亮、禁用日期等逻辑。以下是关键实现步骤: 日期选择与区间高亮 使用date-fns库处理日期操作,实现开始/结束日期选…

react实现日历插件

react实现日历插件

实现日历插件的核心步骤 安装依赖 使用react-datepicker或@mui/x-date-pickers等现成库快速实现日历功能。例如: npm install react-datepicke…

php 实现日历

php 实现日历

使用 PHP 实现日历 基础日历实现 PHP 可以通过日期函数和循环结构实现一个简单的日历。以下是一个基础示例: <?php function generateCalendar($year,…

php实现日历

php实现日历

PHP 实现日历的方法 PHP 可以用于生成动态日历,以下是几种常见的实现方式: 使用 date 和 mktime 函数 通过 PHP 内置的日期和时间函数,可以计算月份的天数和起始星期几。 fu…