Angular ngx日期选择器:设置前几年的初始年份值
我在Angular 8中使用ngx datepicker,我想将年份(日历)从今天往后设置18年。 我用的是这里的例子 此示例设置minDate和maxDate,如何设置初始日期/年份,即打开下拉列表时显示2002年。尝试如下 组件。tsAngular ngx日期选择器:设置前几年的初始年份值,angular,Angular,我在Angular 8中使用ngx datepicker,我想将年份(日历)从今天往后设置18年。 我用的是这里的例子 此示例设置minDate和maxDate,如何设置初始日期/年份,即打开下拉列表时显示2002年。尝试如下 组件。ts import { Component } from '@angular/core'; @Component({ selector: 'demo-datepicker-min-max', templateUrl: './min-max.componen
import { Component } from '@angular/core';
@Component({
selector: 'demo-datepicker-min-max',
templateUrl: './min-max.component.html'
})
export class DemoDatepickerMinMaxComponent {
minDate: Date;
maxDate: Date;
currentData: Date;
constructor() {
this.minDate = new Date();
this.maxDate = new Date();
this.currentData = new Date();
this.currentData.setFullYear(this.minDate.getFullYear() -3);
this.minDate.setFullYear(this.minDate.getFullYear() -3);
console.log(this.minDate);
this.maxDate.setDate(this.maxDate.getDate() + 7);
}
}
component.html
<div class="row">
<div class="col-xs-12 col-12 col-sm-6 col-md-4 form-group">
<input class="form-control"
placeholder="Datepicker"
[(ngModel)]="currentData"
bsDatepicker
[minDate]="minDate"
[maxDate]="maxDate">
</div>
</div>
我已根据文档进行了更改 改变你的html格式
<div class="row">
<div class="col-xs-12 col-12 col-sm-6 col-md-4 form-group">
<input class="form-control"
placeholder="Datepicker"
bsDatepicker
[bsValue]="bsValue"
[minDate]="minDate"
[maxDate]="maxDate">
</div>
</div>
这是输出
希望这对你有帮助
谢谢这是stackblitz的工作
import { Component } from "@angular/core";
@Component({
selector: "demo-datepicker-min-max",
templateUrl: "./min-max.component.html"
})
export class DemoDatepickerMinMaxComponent {
minDate: Date;
maxDate: Date;
bsValue = new Date();
constructor() {
this.minDate = new Date();
this.maxDate = new Date();
this.minDate.setFullYear(this.minDate.getFullYear() - 18);
console.log(this.minDate);
this.maxDate.setDate(this.maxDate.getDate() + 7);
this.bsValue = new Date(
new Date().setFullYear(new Date().getFullYear() - 18)
);
}
}