【发布时间】:2021-09-11 14:08:11
【问题描述】:
我正在使用https://reactdatepicker.com/
现在默认开始日期是当前日期。我希望开始日期是 1970 年的开始年份。我不想禁用以前的日期,只是希望日历显示日期为 1970 年 1 月 1 日,以便用户可以选择出生日期。
这是我当前的代码:
class ReactDatePicker extends React.Component {
constructor(props) {
super(props);
this.state = {
startDate: new Date() - 80
}
this.datepickerRef = React.createRef();
}
setDate = date => {
this.props.onChange(this.props.name, date);
};
handleClickDatepickerIcon = () => {
const datepickerElement = this.datepickerRef;
datepickerElement.setFocus(true);
}
render() {
const { name, value } = this.props;
return (
<Datepicker
id={name}
name={name}
selected={value}
onChange={this.setDate}
startDate={this.state.startDate}
maxDate={new Date()}
popperPlacement="bottom"
dateFormat="MMMM d, yyyy"
showMonthDropdown="true"
showYearDropdown="true"
placeholderText="Select a date"
ref={el => this.datepickerRef = el}
/>
);
}
}
【问题讨论】:
-
如果你想要一个 80 年前的日期,你应该使用
date.setFullYear( date.getFullYear() - 80 ); -
@RST 这实际上删除了占位符文本....
-
我只是表明你的 -80 没有做你认为的事情。
-
@RST 通过将选定的道具更改为上一年的作品。但它隐藏了占位符文本并在那里显示年份。