elementui的时间选择器 禁用事件

复制代码
watch: {
    queryParams: {
      handler (newval) {
        function filltertime (time) {
          return new Date(time).getTime()
        }
        let Newval = newval['yearMonth']
        const _this = this
        // 记录当前年份
        const nowYear = Number(new Date(+this.$getTime).toLocaleDateString().split('/')[0])
        // 记录当前月
        const nowMonth = Number(new Date(+this.$getTime).toLocaleDateString().split('/')[1])
        let CheckMonthJoin = ''
        const CheckMonth = Newval.split('').map((e, i) => {
          if (i <= 3) {
            CheckMonthJoin += e
          } else {
            if (i == 4 && e == 1) {
              CheckMonthJoin += `/${e}`
            } else if (i == 4 && e == 0) {
              CheckMonthJoin += '/'
            }
            if (i == 5) {
              CheckMonthJoin += e
            }
          }
          return CheckMonthJoin
        })[Newval.split('').length - 1]
        /** 记录选择的年份 */
        const checkYear = Number(CheckMonth.split('/')[0])
        /** 记录选择月份 */
        const checkMonth = Number(CheckMonth.split('/')[1])
        this.pickerOptions = {
          disabledDate: (time) => {
            const day = new Date(+this.$getTime).toLocaleDateString()
            const newday = day.split('/')
            let joinday
            let returnBoolean
            if (Newval.constructor === new Date().constructor) {
              Newval = Newval.toLocaleDateString().split('/')[0] + Newval.toLocaleDateString().split('/')[2]
            }
            if (filltertime(Newval) < filltertime(newday[1].length > 1 ? newday[0] + newday[1] : newday[0] + '0' + newday[1])) {
              // 月初
              const Oldday = Newval.split('')
              let joinMonthfirst = ''
              const Monthfirst = Oldday.map((e, i) => {
                if (i <= 3) {
                  joinMonthfirst += e
                } else {
                  if (i == 4 && e == 1) {
                    joinMonthfirst += `/${e}`
                  } else if (i == 4 && e == 0) {
                    joinMonthfirst += '/'
                  }
                  if (i == 5) {
                    joinMonthfirst += e
                  }
                }
                return joinMonthfirst
              })[Oldday.length - 1] + '/1'
              // 月末
              var joinMonthlast = ''
              const Monthlast = Oldday.map((e, i) => {
                if (i <= 3) {
                  joinMonthlast += e
                } else {
                  if (i == 4 && e == 1) {
                    joinMonthlast += `/${e}`
                  } else if (i == 4 && e == 0) {
                    joinMonthlast += `/`
                  }
                  if (i == 5) {
                    if (Oldday[4] == 1 && e == 2) {
                      joinMonthlast += e
                    } else {
                      e = Number(e) + 1
                      joinMonthlast += e
                    }
                  }
                }
                return joinMonthlast
              })[Oldday.length - 1] + '/1'
              // 当月为12月时处理
              const Month_12 = Monthlast.split('/')
              if (Month_12[1] == 12 && Monthfirst.split('/')[1] == 12) {
                Month_12[0] = Number(Month_12[0]) + 1
                Month_12[1] = '1'
                Month_12.join('/')
                returnBoolean = time.getTime() < new Date(Monthfirst).getTime() || time.getTime() >= new Date(Month_12).getTime()
              } else {
                returnBoolean = time.getTime() < new Date(Monthfirst).getTime() || time.getTime() >= new Date(Monthlast).getTime()
              }
              return returnBoolean
            } else {
              newday[2] = 1
              joinday = newday.join('/')
              returnBoolean = time.getTime() < new Date(joinday).getTime() || time.getTime() > this.$getTime
              return returnBoolean
            }
          }
        }
        if (nowYear >= checkYear && nowMonth >= checkMonth) {
          this.MonthchangeNumber = (nowYear - checkYear) * 12 + (nowMonth - checkMonth)
        } else if (nowYear >= checkYear && nowMonth < checkMonth) {
          this.MonthchangeNumber = (nowYear - checkYear) * 12 - (checkMonth - nowMonth)
        } else if (nowYear < checkYear && nowMonth >= checkMonth) {
          this.MonthchangeNumber = -((checkYear - nowYear) * 12 - (nowMonth - checkMonth))
        } else if (nowYear < checkYear && nowMonth < checkMonth) {
          this.MonthchangeNumber = -((checkYear - nowYear) * 12 + (checkMonth - nowMonth))
        }
        function getDefaultValue (MonthchangeNumber) {
          return new Date(_this.$getTime - MonthchangeNumber * 30 * 24 * 60 * 60 * 1000).toLocaleDateString().split('/').map((e, i) => {
            if (i == 2) {
              e = '1'
            }
            return e
          }).join('/')
        }
        this.DefaultValue = getDefaultValue(this.MonthchangeNumber)
      },
      deep: true,
    }
  },

 // 时间选择器范围设置
      pickerOptions: {
        disabledDate: (time) => {
          const day = new Date(+this.$getTime).toLocaleDateString()
          const newday = day.split('/')
          newday[2] = 1
          const joinday = newday.join('/')
          return time.getTime() < new Date(joinday).getTime() || time.getTime() > (this.$getTime ? this.$getTime : Date.now())
        }
      },
      // 月份范围控制
      pickerOptionsMonth: {
        disabledDate: (time) => {
          return time.getTime() > this.$getTime
        }
      },
      // 记录月份选择间隔
      MonthchangeNumber: 0
 
复制代码

 取每月的最后一天,内置对象Date函数可以传递7个参数

// 内置时间对象 Date 可以传递7个参数.分别是 :
Date(年,月,日,时,分,秒,毫秒)
// 要想获取到最后一天,只需要传年月日并且将日-1,也就是0
new Date(2023,3,0).toLocaleDateString()
// 打印结果
'2023/3/31'

 

posted @   行8  阅读(58)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· Manus的开源复刻OpenManus初探
· .NET Core 中如何实现缓存的预热?
· 三行代码完成国际化适配,妙~啊~
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
点击右上角即可分享
微信分享提示