vue实现展示工休日日历(含农历),并可点击切换工休日

还是展示下成品效果
image.png

懒得解释了,直接丢源码吧

里面有个农历的js文件,发私信找我索取
<template>
  <div class="rili">
    <div class="change">
      <span>
        <el-button @click="changeMonthFun(1)">上月</el-button>
      </span>
      <el-date-picker :clearable="false" v-model="date" style="width: 120px;" @change="handleChange" type="month" placeholder="选择年月">
      </el-date-picker>
      <el-button @click="changeMonthFun(2)">下月</el-button>
    </div>
    <div class="rili_mouth">
      <!--components/xx-calendar/xx-calendar.wxml-->
      <!-- 头部 -->
      <div class="title-wrap">
        <div class="week">
          <span v-for="(item, index) in weeksArr" :key="index">{{ item }}</span>
        </div>
      </div>
      <!-- 日期 -->
      <div class="date-wrap">
        <!-- 上个月日期 -->
        <div class="mouth-date last-mouth" v-for="(item, index) in lastMonthDays" :key="index + 'a'">
          <span class="day-span">{{ item.date }}</span>
          <span class="day-nongli">十五</span>
          <span class="day-dot"></span>
        </div>

        <!-- 当月日期 -->
        <div class="mouth-date current-mouth" v-for="(item, index) in nowMonthDays" :key="index"
          @click="selectDate(item)">
          <div class="day-box" :class="item.restDay ? 'active' : ''">
            <span class="day-span">{{ item.date }}</span>
            <span class="day-nongli">{{ item.day }}</span>
            <span class="tip" v-if="item.restDay">休</span>
            <span class="tip" v-else>班</span>
          </div>
        </div>
        <!-- 下个月日期 -->
        <div class="mouth-date next-mouth" v-for="(item, index) in nextMonthDays" :key="index + 'b'">
          <span class="day-span">{{ item.date }}</span>
          <span class="day-nongli">十五</span>
          <span class="day-dot"></span>
        </div>
      </div>

    </div>
  </div>
</template>

<script>
import calendarFormatter from "@/utils/calendar";
export default {
  data() {
    return {
      restDayArr:[],
      year: new Date().getFullYear(),
      month: new Date().getMonth() + 1,
      date: new Date().getFullYear() + '-' + (new Date().getMonth() + 1),
      weeksArr: ['日', '一', '二', '三', '四', '五', '六'],
      nowMonth: new Date().getMonth() + 1, //本月是几月
      nowDay: new Date().getDate(), //本月当天的日期
      lastMonthDays: [], //上一个月
      nowMonthDays: [], //本月 
      nextMonthDays: [], //下一个月
    }
  },
  methods: {
    handleChange(val) {
    this.year = val.getFullYear()
    this.month = val.getMonth() + 1
    this.date = this.year + '-' + this.month
    console.log(this.date);
    this.createDays(this.year,this.month)
    this.getRestDays(this.year,this.month)
  },
    //获取当月天数
    getThisMonthDays(year, month) {
      return new Date(year, month, 0).getDate();
    },
    /** 总方法 */
    //创建日期
    createDays(year, month) {
      this.getLastMonthDays(year, month)
      this.getNowMonthDays(year, month)
      this.getNextMonthDays(year, month)
    },
    /** 获取上个月日期 */
    getLastMonthDays(year, month) {
      let nowMonthFirstDays = new Date(year, month - 1, 1).getDay()
      let lastMonthDays = []
      if (nowMonthFirstDays) { //判断当月的第一天是不是星期天
        //上个月显示多少天
        let lastMonthNums = month - 1 < 0 ? this.getThisMonthDays(year - 1, 12) : this.getThisMonthDays(year, month - 1); //判断是否会跨年
        //上个月从几号开始显示
        for (let i = lastMonthNums - nowMonthFirstDays + 1; i <= lastMonthNums; i++) {
          let time = new Date(year, month - 2, i).toLocaleDateString() //对应的时间
          lastMonthDays.push({
            date: i, //几号
            week: this.weeksArr[new Date(year, month - 2, i).getDay()], //星期几
            time,
            isNowMonthDay: ''
          });
        }
      }
      this.lastMonthDays = lastMonthDays
      console.log(this.lastMonthDays);
    },
    /** 获取当月日期 */
    getNowMonthDays(year, month) {
      let nowMonthDays = []
      let days = this.getThisMonthDays(year, month); //获取当月的天数
      for (let i = 1; i <= days; i++) {
        let d = new Date(year, month - 1, i)
        let years = d.getFullYear()
        let months = d.getMonth() + 1
        let day2 = d.getDate()
        let time = `${years + '/' + months + '/' + day2}` // 2023/3/3
        let timer = time.replace(/\//g, "-")
        let timer2 = timer.split('-')
        var day = calendarFormatter.solar2lunar(timer2[0], timer2[1], timer2[2]);
        let newdate
        if (day.IDayCn == '初一') {
          newdate = day.IMonthCn
        } else {
          newdate = day.IDayCn
        }
        nowMonthDays.push({
          date: i, //几号
          week: this.weeksArr[new Date(year, month - 1, i).getDay()], //星期几
          time,
          restDay: false,
          day: newdate,
        });
      }
      
      this.nowMonthDays = nowMonthDays
      console.log(nowMonthDays);
    },
    /** 获取下个月日期 */
    getNextMonthDays(year, month) {
      let {
        lastMonthDays,
        nowMonthDays,
      } = this
      let nextMonthDays = []
      let nextMonthNums = (lastMonthDays.length + nowMonthDays.length) > 35 ? 42 - (lastMonthDays.length + nowMonthDays.length) : 35 - (lastMonthDays.length + nowMonthDays.length) //下个月显示多少天
      let nowYear = (parseInt(month) + 1) > 12 ? year + 1 : year //下一个月的年份
      let nowMonth = (parseInt(month) + 1) > 12 ? 1 : parseInt(month) + 1 //下一个月的月份
      if (nextMonthNums) { //判断当前天数是否大于零
        for (let i = 1; i <= nextMonthNums; i++) {
          let time = new Date(year, month, i).toLocaleDateString()
          nextMonthDays.push({
            date: i, //几号
            week: this.weeksArr[new Date(nowYear, nowMonth - 1, i).getDay()], //星期几
            time,
            isNowMonthDay: ''
          });
        }
      }

      this.nextMonthDays = nextMonthDays
      console.log(nextMonthDays)
    },
    /** 切换月份 */
    changeMonthFun(e) {
      let {
        year,
        month
      } = this
      let type = e //类型
      if (type == 1) { //上一个月
        year = month - 1 > 0 ? year : year - 1
        month = month - 1 > 0 ? month - 1 : 12
      } else { //next 下个月
        year = (parseInt(month) + 1) > 12 ? year + 1 : year
        month = (parseInt(month) + 1) > 12 ? 1 : parseInt(month) + 1
      }
      this.year = year
      this.month = month
      this.createDays(year, month)
      this.getRestDays(year,month)

    },
    selectDate(item) {
      let date = item.time //选择的下标
      item.restDay = !item.restDay 
      let selectDate = date.replace(/\//g, "-")
      console.log("选择的时间", selectDate)
    },
    dislodgeZero(str) {
      let strArray = str.split("-");
      strArray = strArray.map(function (val) {
        if (val[0] == "0") {
          return (val = val.slice(1));
        } else {
          return val;
        }
      });
      return strArray.join("-");
    },
    getRestDays(year, month) {
      const restDays = [];

      // 获取当月第一天和最后一天的日期对象
      const firstDayOfMonth = new Date(year, month - 1, 1);
      const lastDayOfMonth = new Date(year, month, 0);

      // 循环处理每一天,判断是否为休息日(周六、周日)
      for (let day = 1; day <= lastDayOfMonth.getDate(); day++) {
        const date = new Date(year, month - 1, day);
        const dayOfWeek = date.getDay();

        if (dayOfWeek === 0 || dayOfWeek === 6) {
          // 如果是周六或周日,则将其加入到休息日数组中
          const formattedDate = `${year}-${month}-${day.toString().padStart(2, '0')}`;
          restDays.push(formattedDate);
        }
      }
      console.log(restDays);
      this.restDayArr = restDays
    }
  },
  mounted() {
    this.createDays(this.year, this.month);
    this.getRestDays(2023,5)
  },
  watch: {
    restDayArr(newValue, oldValue) {
      newValue.forEach(ele => {
        ele = this.dislodgeZero(ele).replace(/\-/g, "/")
        this.nowMonthDays.forEach(item => {
          if (ele == item.time) {
            item.restDay = true
          }
        })
      })
    }
  },
 
}
</script>

<style lang="scss" scoped>
.rili {
  border: 1px solid #2ba389;
  background: #eee;
  flex: 1;
  max-width: 544px;
  // min-height: 440px;
  border-radius: 12px;
  display: flex;
  flex-direction: column;

  .change {
    display: flex;
    justify-content: space-around;
    padding: 10px 0;
  }

  .rili_mouth {
    border-top: 1px solid #2ba389;
    border-radius: 16px;
    flex: 1;
    background: #fff;
    padding-bottom: 12px;

    /* 头部样式start */
    .week {
      display: flex;
      justify-content: space-around;
      font-size: 14px;
      color: #1F1F1F;
      padding: 15px 0 10px 0;
    }

    /* 头部样式end */
    /* 日期区域样式start */
    .date-wrap {
      display: flex;
      flex-wrap: wrap;
    }

    .mouth-date {
      display: flex;
      font-size: 16px;
      flex-direction: column;
      align-items: center;
      width: calc(100% / 7);
      padding-top: 10px;
      padding-bottom: 10px;
      cursor: pointer;

    }

    .last-mouth span,
    .next-mouth span {
      opacity: 0;
    }

    .mouth-date .day {
      display: flex;
      flex-direction: column;
      align-items: center;
      color: #1F1F1F;

    }

    .mouth-date .day-nongli {
      font-size: 12px;
      color: #888888;
      margin: 3px 0 6px 0;
    }

    .tip {
      font-size: 12px;
      position: absolute;
      top: 0;
      right: 0;
      background: #4b9ef2;
      color: #fff;
      padding: 2px;
      border-radius: 3px;
    }

    .mouth-date .color {
      color: #8096F0;
    }

    .mouth-date .day-dot {
      width: 5px;
      height: 5px;
      border-radius: 50%;
      background-color: #8096F0;
    }

    .mouth-date .day-box {
      border-radius: 6px;
      padding: 8px 17px;
      display: flex;
      flex-direction: column;
      align-items: center;
      position: relative;
      font-size: 16px;
      min-width: 58px;

    }

    .mouth-date .active {
      background-color: #f7d0cf;
    }

    .mouth-date .active span.tip {
      color: #fff;
      background: red;
    }

    .mouth-date .active span.day-span {
      color: red;
      font-weight: bold;
    }

    .mouth-date .active .day-nongli {
      color: red;
    }

  }
}
</style>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 159,458评论 4 363
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 67,454评论 1 294
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 109,171评论 0 243
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 44,062评论 0 207
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 52,440评论 3 287
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 40,661评论 1 219
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 31,906评论 2 313
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 30,609评论 0 200
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 34,379评论 1 246
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 30,600评论 2 246
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 32,085评论 1 261
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 28,409评论 2 254
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 33,072评论 3 237
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 26,088评论 0 8
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 26,860评论 0 195
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 35,704评论 2 276
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 35,608评论 2 270

推荐阅读更多精彩内容