我有日期过滤器的表单,我尝试设置日期输入的开始日期和结束日期的默认值。
<form [formGroup]="filter" (ngSubmit)="applyFilter()">
<mat-form-field>
<input matInput [matDatepicker]="start" formControlName="start" placeholder="Начальная дата">
<mat-datepicker-toggle matSuffix [for]="start"></mat-datepicker-toggle>
<mat-datepicker #start></mat-datepicker>
</mat-form-field>
<mat-form-field>
<input matInput [matDatepicker]="end" formControlName="end" placeholder="Конечная дата">
<mat-datepicker-toggle matSuffix [for]="end"></mat-datepicker-toggle>
<mat-datepicker #end></mat-datepicker>
</mat-form-field>
以及TS部分
refreshFilter() {
const now = new Date();
const monthAgo = new Date().setMonth(now.getMonth() - 1).toString();
console.log(monthAgo)
console.log(now)
this.filter = new FormGroup({
start: new FormControl(monthAgo, []),
end: new FormControl(now, [])
});
}
一个月前的 console.log()
是 1533908066234
,但新日期是 2018 年 9 月 10 日星期一 16:34:26 GMT+0300
和带有时间戳的表单输入不起作用。如何获取一个月前的正确格式日期以成功设置到 FormControl
中?
最佳答案
如果你想在 Angular 中格式化日期,你可以使用 DatePipe 尝试使用管道进行格式化。 这允许您根据区域设置规则格式化日期值。 如果您需要更多相关信息,您还可以在此处查看: https://angular.io/api/common/DatePipe
本月也执行Ago.toLocaleDateString()
希望这会有所帮助!
关于javascript - 如何在 Angular 6 中设置响应式(Reactive)表单的日期默认值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52259142/