![](/img/trans.png)
[英]Problem with date (react-datepicker) when i paste new date in datepicker input
[英]react-datepicker fails with moment and new Date
我正在尝试通过moment
在react-datepicker组件中设置minDate
属性:
if(this.props.minDate){
console.log('date:' + moment(this.props.minDate)); //1517436000000
return <DatePicker
minDate={ new moment(this.props.minDate)}
selected={this.state.startDate}
onChange={this.handleChange}
disabled = {this.props.disabled}
/>
或使用new Date
:
console.log('new date:' + new Date(this.props.minDate)); // Thu Feb 01 2018 02:00:00 GMT+0200
return <DatePicker
minDate={ new Date(this.props.minDate)}
selected={this.state.startDate}
onChange={this.handleChange}
disabled = {this.props.disabled}
/>
我的minDate字符串- "2011-10-01"
。 结果仍然是当天,并且控制台中没有错误,感谢您的帮助。
我认为您必须为此提供价值,因为minDate和maxDate用于验证目的。 仅当给定值或当前日期小于最小日期时,minDate才是结果值
class Example extends React.Component {
constructor (props) {
super(props)
this.minDate = new moment(props.minDate)
this.state = {
startDate: props.minDate
};
this.handleChange = this.handleChange.bind(this);
}
handleChange(date) {
this.setState({
startDate: date
});
}
render() {
return <DatePicker
selected={this.state.startDate}
onChange={this.handleChange}
minDate={this.minDate}
value={this.props.startDate}
/>;
}
}
在我看来,当您将数据传递给minDate prop时,您不需要瞬间的新实例。 您正在这样做,例如minDate={ new moment(this.props.minDate)}
。
React-datepicker已经在内部使用矩来处理数据对象,因此我认为您应该在将其传递给minDate道具的那一刻之前删除关键字new。
我自己一个人,看来如果设置了selected
属性,它将显示而不是minDate
属性。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.