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)}
                    />

 

 


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM