antd 时间选择器 TimePicker 实现只能选择 当天零点到当前时间的时间段

import { TimePicker } from 'antd';
 
引入   import moment from 'moment';
 
moment().hours() 获取当前小时
moment('2021-08-05 11:07:05').hours()   // 11
 
携带年月日           time[0].format('YYYY-MM-DD HH:mm:ss') 或  time[1].format('YYYY-MM-DD HH:mm:ss')
只展示时分秒        time[0].format('HH:mm:ss') 或  time[1].format('HH:mm:ss')
 
import { useState } from 'react';
import { TimePicker } from 'antd';  // 引入

  const [time, setTime] = useState<any>([]);

  // 时间禁用设置
  // 禁用小时
  const getDisabledHours = () => {
    let hours = [];
    for (var i = 23; i > moment().hours(); i--) {
      hours.push(i);
    }
    return hours;
  };
  // 禁用分钟
  const getDisabledMinutes = (hours) => {
    let minutes = [];
    if (hours === moment().hours()) {
      for (var i = 59; i > moment().minutes(); i--) {
        minutes.push(i);
      }
    }
    return minutes;
  };
  // 禁用秒
  const getDisabledSeconds = (hours, minutes) => {
    let second = [];
    if (hours == moment().hours() && minutes == moment().minutes()) {
      for (var i = 59; i >= moment().seconds(); i--) {
        second.push(i);
      }
    }
    return second;
  };

                    <TimePicker.RangePicker
                      value={time}
                      disabledHours={getDisabledHours}
                      disabledMinutes={getDisabledMinutes}
                      disabledSeconds={getDisabledSeconds}
                      onChange={(val) => setTime(val)}
                    />

 

 

posted @ 2021-10-20 17:45  cielw  阅读(2488)  评论(0)    收藏  举报