js日历日期计算核心代码

16 篇文章 0 订阅
11 篇文章 0 订阅

相信小伙伴有的时候会制作污染日历, 以下是日历的核心算法, 只有js没有样式,可以参考下

Date.prototype.format = function (fmt) {
      let o = {
        "M+": this.getMonth() + 1,                 //月份
        "d+": this.getDate(),                    //日
        "h+": this.getHours(),                   //小时
        "m+": this.getMinutes(),                 //分
        "s+": this.getSeconds(),                 //秒
        "q+": Math.floor((this.getMonth() + 3) / 3), //季度
        "S": this.getMilliseconds()             //毫秒
      };
      if (/(y+)/.test(fmt)) {
        fmt = fmt.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
      }
      for (let k in o) {
        if (new RegExp("(" + k + ")").test(fmt)) {
          fmt = fmt.replace(RegExp.$1, (RegExp.$1.length == 1) ? (o[k]) : (("00" + o[k]).substr(("" + o[k]).length)));
        }
      }
      return fmt;
    }

    function initCalendar(year, month) {
      //注: js中的月份是从0开始的,
      //获取当月的第一天是星期几
      let firstDate = new Date(year, month - 1).getDay();
      //获取当月的最后一天是几号
      let lastDate = new Date(year, month, 0).getDate();
      //做日期头的转换, getDay()方法默认星期天为0
      firstDate = firstDate - 1 < 0 ? 6 : firstDate - 1
      let dateArr = [];
      //添加前一个月剩下的日期
      for (let i = 0; i < firstDate; i++) {
        let timeStamp = new Date(year, month - 1, 1).getTime() - 86400000 * (firstDate - i);
        let day = new Date(timeStamp).getDate();
        dateArr.push({
          inMonth: false,
          day,
          date: new Date(timeStamp).format('yyyy-MM-dd')
        })
      }
      //添加当月剩下的日期
      for (let i = 1; i < lastDate + 1; i++) {
        let day = new Date(year, month - 1, i).getDate();
        dateArr.push({
          inMonth: true,
          day,
          date: new Date(year, month - 1, i).format('yyyy-MM-dd')
        })
      }
      //添加下月日期
      let dateCount = 42 //日历总共有多少天
      let d_len = dateArr.length // 已经添加日期的数组长度
      for (let i = 0; i < dateCount - d_len; i++) {
        let timeStamp = new Date(year, month, 1).getTime() + 86400000 * i;
        let day = new Date(timeStamp).getDate();
        dateArr.push({
          inMonth: false,
          day,
          date: new Date(year, month, i + 1).format('yyyy-MM-dd')
        })
      }
      return dateArr
    }

    initCalendar(2022, 6)

输出:

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是一个使用C语言实现教学日历的示例代码,包括学期时间计算、课程表生成和课程时间表计算核心算法: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #define MAX_WEEK_DAYS 7 #define MAX_WEEKS 20 // 学期开始日期 struct tm start_date = { .tm_year = 2022 - 1900, .tm_mon = 8, .tm_mday = 1 }; // 学期结束日期 struct tm end_date = { .tm_year = 2023 - 1900, .tm_mon = 0, .tm_mday = 20 }; // 课程表 const char* course_table[MAX_WEEKS][MAX_WEEK_DAYS] = { {"", "数学", "语文", "", "", "", ""}, {"", "英语", "数学", "音乐", "", "", ""}, {"", "化学", "", "数学", "", "", ""}, {"", "物理", "", "数学", "", "", ""}, {"", "语文", "", "数学", "生物", "", ""}, {"", "英语", "", "化学", "数学", "", ""}, {"", "", "数学", "物理", "", "体育", ""}, {"", "", "语文", "历史", "", "化学", ""}, {"", "", "物理", "英语", "", "数学", ""}, {"", "", "数学", "语文", "", "英语", ""}, {"", "", "英语", "物理", "", "化学", ""}, {"", "", "生物", "数学", "语文", "英语", ""}, {"", "", "", "英语", "数学", "生物", ""}, {"", "", "", "化学", "英语", "历史", ""}, {"", "", "", "数学", "语文", "音乐", ""}, {"", "", "", "物理", "英语", "数学", ""}, {"", "", "", "英语", "数学", "化学", ""}, {"", "", "", "语文", "物理", "数学", ""}, {"", "", "", "数学", "英语", "生物", ""}, {"", "", "", "音乐", "数学", "语文", ""} }; // 根据日期计算周几 int calculate_weekday(struct tm date) { time_t t = mktime(&date); struct tm* tm = localtime(&t); return tm->tm_wday; } // 根据学期日期计算所有日期 void calculate_all_dates(struct tm start_date, struct tm end_date, struct tm** all_dates, int* num_dates) { int diff = difftime(mktime(&end_date), mktime(&start_date)) / 86400; *num_dates = diff + 1; *all_dates = (struct tm*)malloc(sizeof(struct tm) * (*num_dates)); struct tm current_date = start_date; for (int i = 0; i < *num_dates; i++) { (*all_dates)[i] = current_date; current_date.tm_mday++; mktime(&current_date); } } // 生成课程表 void generate_course_table(struct tm* all_dates, int num_dates) { struct tm current_date; int current_week = 0; int current_weekday = 0; for (int i = 0; i < num_dates; i++) { current_date = all_dates[i]; current_weekday = calculate_weekday(current_date); if (current_weekday == 0) { current_week++; printf("\n第%d周\n", current_week); } printf("%d月%d日 %s\n", current_date.tm_mon + 1, current_date.tm_mday, course_table[current_week - 1][current_weekday]); } } // 计算课程时间表 void calculate_course_timetable(struct tm start_date, struct tm** course_timetable, int num_weeks, int num_weekdays) { *course_timetable = (struct tm*)malloc(sizeof(struct tm) * num_weeks * num_weekdays); struct tm current_date = start_date; int current_week = 0; int current_weekday = 0; for (int i = 0; i < num_weeks * num_weekdays; i++) { (*course_timetable)[i] = current_date; current_weekday++; if (current_weekday >= num_weekdays) { current_week++; current_weekday = 0; } current_date.tm_mday += 7; mktime(&current_date); } } int main() { int num_dates; struct tm* all_dates; calculate_all_dates(start_date, end_date, &all_dates, &num_dates); generate_course_table(all_dates, num_dates); struct tm* course_timetable; calculate_course_timetable(start_date, &course_timetable, MAX_WEEKS, MAX_WEEK_DAYS); free(all_dates); free(course_timetable); return 0; } ``` 该示例代码实现了教学日历核心算法,包括了学期时间计算、课程表生成和课程时间表计算等功能。其中,calculate_weekday函数用于根据日期计算周几,calculate_all_dates函数用于根据学期日期计算所有日期,generate_course_table函数用于生成课程表,calculate_course_timetable函数用于计算课程时间表。通过这些算法,可以实现一个基本的教学日历应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值