zoukankan      html  css  js  c++  java
  • antd design DatePicker RangePicker限制时间范围,其中结束时间限制时分秒不可选

    原文来自:https://blog.csdn.net/liuliuliuliumin123/article/details/103044141?utm_medium=distribute.pc_aggpage_search_result.none-task-blog-2~all~first_rank_v2~rank_v25-3-103044141.nonecase&utm_term=disabledtime%E4%B8%8D%E5%8F%AF%E9%80%89%E6%8B%A9%E7%9A%84%E6%97%B6%E9%97%B4

    需求:选择时间区间,且只能选择当前时刻之前的时间,且时分秒也要做限制。(比如现在是2019-11-13 10:29:31,那就只能选择此刻以前的,尚未发生的时间不可选。)期望图如下:

    实现:

    1. 首先在页面引入时间控件与moment插件

    import { DatePicker } from 'antd';

    import moment from 'moment';

    const { RangePicker } = DatePicker;

    2. 在render中使用时间控件。

    限制日期不可选是disabledDate,限制时间不可选是disabledTime(我是与form表单一起使用,可根据情况自行选择)

    <FormItem label="回溯时间区间" {...formItemLayout}>

            {getFieldDecorator("time",{

                rules: [{

                  required: true,

                  message: "请选择回溯区间"

                }],

              })(

              <RangePicker

                disabledDate={this.disabledDate} // 限制日期不可选

                disabledTime={this.disabledDateTime} // 限制时间不可选

                format="YYYY-MM-DD HH:mm:ss" // 时间格式

                placeholder={['开始时间', '结束时间']}

                showTime  // 增加时间选择按钮

              />

              )}

            </FormItem>

    3. 用函数做具体的逻辑判断。

    disabledDate=(current)=>{

        return current && current >= moment().endOf('day'); // 选择时间要大于等于当前天。若今天不能被选择,去掉等号即可。

      }

      range = (start, end) => {

        const result = [];

        for (let i = start; i <= end; i++) {

          result.push(i);

        }

        return result;

      };

      disabledDateTime = (dates,partial) => {

        let hours = moment().hours();//0~23

        let minutes = moment().minutes();//0~59

        let seconds = moment().seconds();//0~59

        //当日只能选择当前时间之后的时间点

        if (dates&&moment(dates[1]).date() === moment().date()&&partial=='end') {

          return {

            disabledHours: () => this.range(hours+1,23),

            disabledMinutes: () => this.range(minutes+1,59),

            disabledSeconds: () => this.range(seconds+1,59),

          };

        }

      }

  • 相关阅读:
    解决web网页访问慢的问题
    Django安装配置(for Mac)
    Django安装(for Mac)
    HTML5中的新事件
    关于http-equiv
    【转】@fant-face
    textarear中的value....还是...innertext
    清除浮动的元素的margin-top触碰不到,浮动元素的边界
    常用排序算法总结(一)
    JS常用特效方法总结
  • 原文地址:https://www.cnblogs.com/liujinyu/p/13534119.html
Copyright © 2011-2022 走看看