![](/img/trans.png)
[英]ng-bootstrap DatePicker set startDate and minDate on trigger Angular 4
[英]How to set minDate for ng-bootstrap datepicker
下面是我用来设置 minDate 的代码
<input ngbDatepicker #d="ngbDatepicker" [minDate]="minDate" [formControl]="Form.controls['dateOfBirth']">
在组件中,我将 minDate 设置如下:
public minDate: Date = void 0;
constructor() {
this.minDate = new Date(1950, 1, 1);
}
但日期选择器仍将 2007 显示为 minDate。 有没有遗漏的东西。 如果需要任何其他信息,请告诉我。
谢谢你的时间。
从您的代码片段看来,您正在使用https://ng-bootstrap.github.io/#/components/datepicker 中的ng-bootstrap 。 假设我是对的,您需要使用NgbDateStruct
(一个包含year
, month
, day
等字段的对象)而不是内置的 JavaScript Date
对象。 例如,要将最小日期设置为今年年初,您可以使用:
this.minDate = {year: 2017, month: 1, day: 1};
这是 plunker 中的一个工作示例: http ://plnkr.co/edit/VDEYDwp7QIZDDHatCNPh?p=preview
只需通过[minDate]="{year: 1980, month: 1, day: 1}"
:
<input type="text" [(ngModel)]="date" ngbDatepicker #d="ngbDatepicker" (focus)="d.open()" [minDate]="{year: 1980, month: 1, day: 1}" name="date" class="form-control"/>
这是你的 HTML 代码
<mat-form-field class="col-md-3">
<mat-label >Date :</mat-label>
<input
matInput
ngxDaterangepickerMd
formControlName="dateFilter"
placeholder="Choose date" [maxDate]="maxDate" [minDate]="minDate"
[showDropdowns]="true"
[singleDatePicker]="true"
[showCancel]="true"
[locale]="locale"
/>
</mat-form-field>
这转到打字稿:
public minDate = moment().subtract(1, 'days');
public maxDate = moment();
selected = moment();
locale :LocaleConfig= {
applyLabel: 'Apply',
customRangeLabel: ' - ',
daysOfWeek: moment.weekdaysMin(),
monthNames: moment.monthsShort(),
firstDay: moment.localeData().firstDayOfWeek(),
};
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.