我需要限制日期选择器,以使16岁以下的人直到16岁那一天才可以在日历上选择生日。
我正在使用Angular datepicker,并且在我的打字稿文件中使用此代码没问题:
now = new Date();
year = this.now.getFullYear();
month = this.now.getMonth();
day = this.now.getDay();
minDate = { year: this.year - 100, month: this.month, day: this.day };
maxDate = { year: this.year - 16, month: this.month + 1, day: this.day };
这是我的HTML:
type="date" maxDate="maxDate" minDate="minDate"
jhiTranslate="form.birthdate.placeholder"
ngModel name="birthdate" #birthdate="ngModel">
我遇到的问题是默认的html datepicker,我需要使用此datepicker而不是ngb-datepicker。我找到的所有解决方案都使用jQuery,但无法在Angular 6中使用。
希望我能说清楚,您可以帮我解决这个问题!
谢谢。
ETA:
这是我的问题的解决方案:
app.component.ts:
import { Component } from '@angular/core';
import moment from 'moment'
@Component({
selector: 'my-app',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
name = 'Angular';
now = new Date();
year = this.now.getFullYear();
month = this.now.getMonth();
day = this.now.getDay();
minDate = moment({year: this.year - 100, month: this.month, day: this.day}).format('YYYY-MM-DD');
maxDate = moment({year: this.year - 16, month: this.month, day: this.day}).format('YYYY-MM-DD');
date(ev) {
console.log(this.minDate)
console.log(ev.target.value)
}
}
HTML:
type="date" [max]="maxDate" [min]="minDate"
jhiTranslate="form.birthdate.placeholder"
ngModel name="birthdate" #birthdate="ngModel">