[英]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}
/>
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我已将其添加到您的代码框中并且它不会崩溃
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.