234234234

vue moment 生成一个7x6=42格子的日历数据

 

 

calc(val = new Date()) {
      const m = moment(val).startOf('month');
      const format = 'yyyy-MM-DD';
      // 获取1号是星期几, 星期日的值为7
      const wd = m.weekday() > 0 ? m.weekday() : 7;
      // 计算距离星期一差几天
      const dd = Math.abs(wd - 1);
      // 获取开始时间
      const startDate = m.clone().subtract(dd, 'day');
      // 获取7x6=42个日期
      const dates = new Array(42).fill(0).map((_, index) => {
        const date = startDate.clone().add(index, 'day');

        return {date, format: date.format(format), nowMonth: index >= dd && index < (m.daysInMonth() + dd)}
      });
      this.$set(this, 'dates', dates);
      // this.dates.splice(0, this.dates.length, dates);
      console.log(this.dates);
    }

 

posted @ 2023-05-18 14:44  你若愿意,我一定去  阅读(37)  评论(0编辑  收藏  举报
23423423423