React Reactstrap 日期表 18 岁或以上
React reactstrap date form age 18 or older
我正在从事一个有表格的项目。我正在为此使用 Reacttsrap。其中一个字段是用户的日期。我需要使用允许 18 岁或以上用户的逻辑。此外,禁用未来日期。 reactstrap 文档中对此没有太多解释。另外,我在网上看到了很多例子,但我还没有看到一个符合这些要求并使其起作用的。
目前,我有以下形式的代码:
<FormGroup>
<Input
id='user-age'
name='date'
type='date'
aria-label='Date of birth'
value={this.state.date}
// onChange={(event) => this.handleInputChange(event)}
/>
<Label for='user-age'>*Date of birth (DD/MM/YY)</Label>
</FormGroup>
评论的代码只是一个让它工作的处理程序的想法。任何帮助将不胜感激。
我刚找到一个答案from here,它帮助我解决了问题。
function getAge(dateString: string | number | Date) {
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}
我正在从事一个有表格的项目。我正在为此使用 Reacttsrap。其中一个字段是用户的日期。我需要使用允许 18 岁或以上用户的逻辑。此外,禁用未来日期。 reactstrap 文档中对此没有太多解释。另外,我在网上看到了很多例子,但我还没有看到一个符合这些要求并使其起作用的。 目前,我有以下形式的代码:
<FormGroup>
<Input
id='user-age'
name='date'
type='date'
aria-label='Date of birth'
value={this.state.date}
// onChange={(event) => this.handleInputChange(event)}
/>
<Label for='user-age'>*Date of birth (DD/MM/YY)</Label>
</FormGroup>
评论的代码只是一个让它工作的处理程序的想法。任何帮助将不胜感激。
我刚找到一个答案from here,它帮助我解决了问题。
function getAge(dateString: string | number | Date) {
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
}
return age;
}