![](/img/trans.png)
[英]Bootstrap Date Range Picker -> Single Date Picker: How to set a Date
[英]How to set max date in date picker?
我正在尝试在日期选择器中设置最大日期得到错误
我正在使用这个日期选择器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 年前
new Date(new Date().setDate(new Date().getDate()-365))
此行将返回正好 365 天前的日期。
我不是使用 momment.js 的专家,但根据文档: https://momentjs.com/guides/#/lib-concepts/mutability/
也许您需要使用.format()
。 我已将其添加到您的代码框中并且它不会崩溃
如果您设置的范围为 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;
要在一个月后获取日期,您可以使用它
const currentDate = new Date()
const calculateValidDate = new Date(currentDate.setMonth(currentDate.getMonth() + 1))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.