【问题标题】:How to set max date in date picker?如何在日期选择器中设置最大日期?
【发布时间】:2019-10-21 11:57:16
【问题描述】:

我正在尝试在日期选择器中设置最大日期,但出现错误

我正在使用这个日期选择器 https://www.npmjs.com/package/semantic-ui-calendar-react

render() {
    return (
      <DateInput
        name="date"
        placeholder="Date"
        // this works
        // maxDate={moment()}
       // this is not working
        maxDate={moment().subtract(1,'years')}
        value={this.state.date}
        iconPosition="left"
        onChange={this.handleChange}
      />
    );
  }

这是我的代码

https://codesandbox.io/s/semantic-ui-example-v9v03

我正在尝试将最大日期设置为 1 年前

【问题讨论】:

  • 检查一下,伙计.. 关注这个jsfiddle.net/raj53/op8v0yaf
  • @chandukomati 是 jquery,而不是 OP 正在寻找的
  • 当前选择的日期将至少增加 3 个月 TODate 设置。

标签: javascript reactjs react-datepicker


【解决方案1】:

您需要在 [minDate, maxDate] 间隔内设置一个initialDate。

<DateInput
  maxDate={moment().subtract(1, "years")}
  initialDate={moment().subtract(1, "years")} <==
  value={this.state.date}
/>

Demo

Source(不知道为什么他们的文档中没有提到这一点)。

【讨论】:

    【解决方案2】:
    new Date(new Date().setDate(new Date().getDate()-365))
    

    此行将返回正好 365 天前的日期。

    【讨论】:

      【解决方案3】:

      我不是使用 momment.js 的专家,但根据文档:https://momentjs.com/guides/#/lib-concepts/mutability/

      也许您需要使用.format()。我已将其添加到您的代码框中,它不会崩溃

      【讨论】:

        【解决方案4】:
        Hello please below example:
        
        import React from "react";
        
        import { DateInput } from "semantic-ui-calendar-react";
        
        const currentdate = new Date();
        const currentYear = currentdate.getFullYear();
        const maxdate = new Date(currentdate.setYear(currentdate.getFullYear() + 1));
        class DateTimeForm extends React.Component {
          constructor(props) {
            super(props);
        
            this.state = {
              date: "",
              time: "",
              dateTime: "",
              datesRange: ""
            };
          }
        
          handleChange = (event, { name, value }) => {
            if (this.state.hasOwnProperty(name)) {
              this.setState({ [name]: value });
            }
          };
        
          render() {
            return (
              <DateInput
                dateFormat="DD - MM - YYYY"
                name="date"
                placeholder="Date"
                maxDate={maxdate}
                value={this.state.date}
                iconPosition="left"
                onChange={this.handleChange}
              />
            );
          }
        }
        export default DateTimeForm;
        

        【讨论】:

          【解决方案5】:

          要在一个月后获取日期,您可以使用它

           const currentDate = new Date()
            const calculateValidDate = new Date(currentDate.setMonth(currentDate.getMonth() + 1))
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-10-02
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2015-04-12
            • 1970-01-01
            • 2018-05-22
            相关资源
            最近更新 更多