简体   繁体   English

如何在日期选择器中设置最大日期?

[英]How to set max date in date picker?

I am trying to set max date in date picker getting error this我正在尝试在日期选择器中设置最大日期得到错误

I am using this date picker https://www.npmjs.com/package/semantic-ui-calendar-react我正在使用这个日期选择器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}
      />
    );
  }

here is my code这是我的代码

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

I am trying to set max date 1 year before我正在尝试将最大日期设置为 1 年前

You need to set an initialDate inside [minDate, maxDate] interval.您需要在 [minDate, maxDate] 间隔内设置一个initialDate

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

Demo演示

Source (not sure why this isn't mentioned on their documentation). 来源(不知道为什么他们的文档中没有提到这一点)。

new Date(new Date().setDate(new Date().getDate()-365))

This line will return the date from exactly 365 days ago.此行将返回正好 365 天前的日期。

I'm not an expert using momment.js but according the docs: https://momentjs.com/guides/#/lib-concepts/mutability/我不是使用 momment.js 的专家,但根据文档: https://momentjs.com/guides/#/lib-concepts/mutability/

maybe you need to use .format() .也许您需要使用.format() I've added it in your codesanbox and it doesn't crash我已将其添加到您的代码框中并且它不会崩溃

If you're setting a range of 1 year you could do the following, sample :如果您设置的范围为 1 年,您可以执行以下操作,示例

<DateInput
    maxDate={moment()}
    minDate={moment().subtract(1, "y")}
    value={this.state.date}
/>
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;

To get date after one month you can use this要在一个月后获取日期,您可以使用它

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Bootstrap Date Range Picker -&gt; Single Date Picker: 如何设置日期 - Bootstrap Date Range Picker -> Single Date Picker: How to set a Date 从客户端设置剑道日期选择器的最大日期 - set the max date on kendo date picker from client side 剑道日期选择器最大日期 - Kendo Date Picker Max Date 如何设置jQuery日期选择器的最大年份限制为当年之前的3年 - how to set the jQuery date picker max year limit is 3 years before of current year 如何在第一个日期选择器选择DATE时在第二个日期选择器中设置DATE? - How to set DATE in second date-picker on selection of DATE from first date-picker? 如何在 angular 中设置日期选择器的默认范围 - How to set default range of date picker in angular 如何使用Clean jQuery Date and Time Picker Plugin设置从一个选择器到另一个选择器的日期-datetimepicker - How to set a date From one picker to another picker using Clean jQuery Date and Time Picker Plugin - datetimepicker 如何为日期范围选择器设置最小日期范围限制 - How to set a minimum date range limit for the Date Range Picker 如何使用jQuery UI日期选择器设置当前日期? - how to set current date using jquery ui date picker? 如何为Sencha Touch的日期选择器设置日期 - How to set date to sencha touch's date picker
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM