使用vue+ElementUI+dayjs实现简单的日历控件

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <link rel="stylesheet" href="./index.css" />
    <script src="./vue.js"></script>
    <script src="./index.js"></script>
    <script src="./dayjs.min.js"></script>
  </head>
  <body>
    <div id="myApp">
      <div class="block">
        <span class="demonstration"></span>
        <el-date-picker
          @change="changeValue"
          v-model="value2"
          type="month"
          placeholder="选择月"
        >
        </el-date-picker>
      </div>
      <table>
        <tr v-for="weekNum in weeks">
          <td v-for="dayNum in 7">{{ getNum(weekNum,dayNum) }}</td>
        </tr>
      </table>
    </div>
    <script>
      new Vue({
        el: "#myApp",
        data() {
          return {
            value2: "",
            weeks: 0,
            count1: 0,
            count2: 0,
            day2: 0,
            day1: 0,
            currentDay: 0,
          };
        },
        methods: {
          changeValue(res) {
            let fullYear = res.getFullYear();
            let month = res.getMonth();
            let daysInMonth = dayjs(res).daysInMonth();
            //当月第一天属于周几
            this.day1 =
              new Date(fullYear, month).getDay() === 0
                ? 7
                : new Date(fullYear, month).getDay();
            //当月最后一天属于周几
            this.day2 =
              new Date(fullYear, month, daysInMonth).getDay() === 0
                ? 7
                : new Date(fullYear, month, daysInMonth).getDay();
            //当月第一天在当周有几天不属于本月
            this.count1 = this.day1 - 1;
            //当月最后一天在当周有几天不属于本月
            this.count2 = 7 - this.day2;
            //本月+同周不同月的天数  共多少天
            let totalDays = daysInMonth + this.count1 + this.count2;
            //本月共跨多少周
            this.weeks = totalDays / 7;

            console.log(
              fullYear,
              month,
              daysInMonth,
              this.day1,
              this.day2,
              this.count1,
              this.count2,
              totalDays,
              this.weeks
            );
          },
          getNum(weekNum, dayNum) {
            if (weekNum === 1) {
              if (dayNum <= this.count1) {
                return "";
              } else {
                return dayNum - this.count1;
              }
            } else if (weekNum === this.weeks) {
              if (dayNum > this.day2) {
                return "";
              } else {
                return 7 - this.count1 + dayNum + (weekNum - 2) * 7;
              }
            } else {
              return 7 - this.count1 + dayNum + (weekNum - 2) * 7;
            }
          },
        },
      });
    </script>
  </body>
</html>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值