我正在做一個有表格的專案。我正在使用 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>
注釋代碼只是使其作業的處理程式的想法。任何幫助將不勝感激。
uj5u.com熱心網友回復:
我剛剛從這里找到了一個答案,它幫助我解決了這個問題。
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;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/437168.html
標籤:javascript 反应 形式 反应带
