VUE elementUI calendar 根據日期段標記日期

RightHereWaitingFY發表於2020-10-09
<template>
   <div class='outer'>
     <main>
       <el-calendar value="2020-10">
          <template slot="dateCell" slot-scope="{date, data}">
            <!-- {{data.day}} -->
            <p :class="data.isSelected ? 'is-selected' : ''">
              {{ data.day.split('-').slice(1).join('-') }} {{ data.isSelected ? '✔️' : ''}}
            </p>
            <div v-if="dayCurrent(data.day)" style="height:5px; background-color: #7fbeff;"></div>
            <div v-else style="height:5px; background-color: #fff;"></div>
          </template>
        </el-calendar>
     </main>
   </div>
</template>
<script>
export default {
  name: '',
  data () {
    return {
      dayList: [],
    }
  },
  methods: {
    dayCurrent (val) {
      console.log(this.dayList.indexOf(val))
      if (this.dayList.indexOf(val) !== -1) {
        return true
      } else {
        return false
      }
    },
    format (time) {
      let ymd = ''
      const mouth = (time.getMonth() + 1) >= 10 ? (time.getMonth() + 1) : ('0' + (time.getMonth() + 1))
      const day = time.getDate() >= 10 ? time.getDate() : ('0' + time.getDate())
      ymd += time.getFullYear() + '-' // 獲取年份。
      ymd += mouth + '-' // 獲取月份。
      ymd += day // 獲取日。
      return ymd // 返回日期。
    },
    getAllDate (start, end) {
      const dateArr = []
      const startArr = start.split('-')
      const endArr = end.split('-')
      const db = new Date()
      db.setUTCFullYear(startArr[0], startArr[1] - 1, startArr[2])
      const de = new Date()
      de.setUTCFullYear(endArr[0], endArr[1] - 1, endArr[2])
      const unixDb = db.getTime()
      const unixDe = de.getTime()
      let stamp
      const oneDay = 24 * 60 * 60 * 1000
      for (stamp = unixDb; stamp <= unixDe;) {
        dateArr.push(this.format(new Date(parseInt(stamp))))
        stamp = stamp + oneDay
      }
      return dateArr
    }
  },
  created () {
    this.dayList = this.getAllDate('2020-10-01', '2020-10-15')
  }
}
</script>
<style lang='less' scoped>
</style>

相關文章