antd的dateTimePicker限制【年月日时分秒】的选择

1.限制年月日

const disabledDate = current => {
  if (current) {
    if (extra?.activityTime && extra?.activityTime?.length !== 0) {
      const currentDate = dayjs(current).format('YYYY-MM-DD')
      const startDate = extra?.activityTime?.[0].slice(0, 10)
      const endDate = extra?.activityTime?.[1].slice(0, 10)
      return !dayjs(currentDate).isBetween(
        dayjs(startDate).subtract(1, 'day'),
        dayjs(endDate).add(1, 'day')
      )
    }
  }
}

2.限制时分秒

const range = (start, end) => {
  const result = []
  for (let i = start; i < end; i++) {
    result.push(i)
  }
  return result
}
// 将时间的时分秒分割出来
const ymd = time => {
  if (time) {
    return [Number(time.slice(11, 13)), Number(time.slice(14, 16)), Number(time.slice(17, 19))]
  }
}
// 禁用时分秒
const disabledSMS = (limitTime, selectTime) => {
  if (limitTime?.[0] === selectTime?.[0] && limitTime?.[1] === selectTime?.[1]) {
    return {
      disabledHours: () => range(0, 24).splice(0, limitTime[0]),
      disabledMinutes: () => range(0, 60).splice(0, limitTime[1]),
      disabledSeconds: () => range(0, 60).splice(0, limitTime[2])
    }
  }
  if (limitTime?.[0] === selectTime?.[0]) {
    return {
      disabledHours: () => range(0, 24).splice(0, limitTime[0]),
      disabledMinutes: () => range(0, 60).splice(0, limitTime[1])
    }
  }
  return {
    disabledHours: () => range(0, 24).splice(0, limitTime[0])
  }
}
// 限制时分秒
const disabledTime = (current, loc) => {
  if (extra?.activityTime && extra?.activityTime?.length !== 0) {
    const start = extra?.activityTime?.[0].slice(0, 10)
    const end = extra?.activityTime?.[1].slice(0, 10)
    const date = current && dayjs(current)?.format('YYYY-MM-DD')
    if (date) {
      if (loc === 'start') {
        if (start === date) {
          const startymd = ymd(extra?.activityTime?.[0])
          const dateymd = ymd(dayjs(current)?.format('YYYY-MM-DD HH:mm:ss'))
          return disabledSMS(startymd, dateymd)
        }
        if (end === date) {
          const endymd = ymd(extra?.activityTime?.[1])
          const dateymd = ymd(dayjs(current)?.format('YYYY-MM-DD HH:mm:ss'))
          return disabledSMS(endymd, dateymd)
        }
      }
    }
  }
  return false
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值