基于element-ui 日期选择器el-date-picker, 即对日期做区间限制

需求:

有时候需求会让我们对日期选择器做限制,即控制最多可跨越多少个月份,其中涉及到不同年份该如何计算。

HTML:

复制代码
<el-date-picker
   v-model="timePeriod"
   type="monthrange"
   value-format="yyyyMM"
   format="yyyyMM"
   range-separator="至"
   start-placeholder="开始月份"
   end-placeholder="结束月份"
>
</el-date-picker>

功能代码:

复制代码
this.timePeriod = ['201402','201409'] //演示数据
      let stagingList = []
      if(this.timePeriod && this.timePeriod.length >0){
          let time = new Date().getFullYear().toString()
          let lists = []

          let yearComparison_1 = this.timePeriod[0].substr(0,time.length)
          let yearComparison_2 = this.timePeriod[1].substr(0,time.length)

          let compareTheMonthsTo_1 = this.timePeriod[0].substr(time.length,2)
          let compareTheMonthsTo_2 = this.timePeriod[1].substr(time.length,2)
          let monthlyDifference = compareTheMonthsTo_2 - compareTheMonthsTo_1

          if (yearComparison_1 == yearComparison_2) {
              if(monthlyDifference == 0){
                  let deduplication = []
                  for (let k = 0; k < this.timePeriod.length; k++) {
                    if (deduplication.indexOf(this.timePeriod[k]) === -1) {
                      deduplication.push(this.timePeriod[k])
                    }
                  }
                  lists = deduplication
              } else if(monthlyDifference > 0){
                  let incrementalStorage = []
                  for (let j = 0; j < monthlyDifference + 1; j++){
                    let moon = (compareTheMonthsTo_1 * 1)  + j
                    if(moon < 10){
                      moon = "0" + moon;
                    }
                    incrementalStorage.push(yearComparison_1 + moon)
                  }
                  lists = incrementalStorage
              }
          }else if(yearComparison_1 != yearComparison_2){
              let length_1 = 12 -compareTheMonthsTo_1
              let storingData1 = []
              let length_2 = compareTheMonthsTo_2 * 1
              let storingData2 = []
              let storingData3 = []
              for (let i = 0; i < length_1 + 1; i++) {
                let moon_1 = (compareTheMonthsTo_1 * 1)  + i
                if(moon_1 < 10){
                  moon_1 = "0" + moon_1;
                }
                storingData1.push(yearComparison_1 + moon_1)
              }

              let th = yearComparison_2 - yearComparison_1
              if(th > 1){
                  var newYearSEve = []
                  let N = yearComparison_1
                  for (let index = 1; index < th; index++) {
                    N = (N * 1) + index
                    for (let q = 1; q < 13; q++) {
                      if(q < 10){
                        q = "0" + q;
                      }
                      newYearSEve.push(N.toString() + q)
                    }
                  }
                  storingData1.push(...newYearSEve);
              }

              for (let j = 0; j < length_2; j++) {
                let moon_2 = j + 1
                if(moon_2 < 10){
                  moon_2 = "0" + moon_2;
                }
                storingData2.push(yearComparison_2 + moon_2)
              }
              storingData3 = storingData1.concat(storingData2)
              lists = storingData3  
          }
          stagingList = lists
      }

      if (stagingList.length > 13) {
          this.$message.warning('账期至多只可选择13个月!')
          return
      }

打印结果:

console.log(stagingList);

相关推荐
小声读源码1 分钟前
【技巧】dify前端源代码修改第一弹-增加tab页
前端·pnpm·next.js·dify
假客套10 分钟前
2025 后端自学UNIAPP【项目实战:旅游项目】7、景点详情页面【完结】
前端·uni-app·旅游
程序员小张丶22 分钟前
基于React Native开发HarmonyOS 5.0主题应用技术方案
javascript·react native·react.js·主题·harmonyos5.0
Captaincc25 分钟前
Ilya 现身多大毕业演讲:AI 会完成我们能做的一切
前端·ai编程
teeeeeeemo37 分钟前
Vue数据响应式原理解析
前端·javascript·vue.js·笔记·前端框架·vue
Sahas101941 分钟前
__VUE_PROD_HYDRATION_MISMATCH_DETAILS__ is not explicitly defined.
前端·javascript·vue.js
Jinxiansen02111 小时前
Vue 3 实战:【加强版】公司通知推送(WebSocket + token 校验 + 心跳机制)
前端·javascript·vue.js·websocket·typescript
MrSkye1 小时前
React入门:组件化思想?数据驱动?
前端·react.js·面试
BillKu1 小时前
Java解析前端传来的Unix时间戳
java·前端·unix
@Mr_LiuYang1 小时前
网页版便签应用开发:HTML5本地存储与拖拽交互实践
前端·交互·html5·html5便签应用