我从数据库中获取日期时遇到问题。我从我的网络服务获取字符串格式的日期,然后我想显示我的日期选择器的输入,但日期没有显示在我的字段中。当我安慰生日时,就像这样02/04/1997 00:00:00。如何将日期格式化为datepicker格式?
这是我的HTML
<div class="mb-24" fxLayout="row">
<mat-form-field fxFlex>
<input matInput [(ngModel)]="client.Birthday" formControlName="Birthday" placeholder="Ditëlindja" [matDatepicker]="picker" tabindex="3">
<mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
<mat-datepicker #picker></mat-datepicker>
</mat-form-field>
</div>
当我得到包括生日的客户列表时,这是我的apiService
Client(): Observable<Client[]> {
return this._http.get<Client[]>('http://localhost:1636/BOXService.asmx/Client');
}
这是我的档案文件.ts
createClientForm() {
return this.formBuilder.group({
Birthday: [this.client.Birthday],
这是我的适配器
const dateFormat = "DD/MM/YYYY";
export class AppDateAdapter extends NativeDateAdapter {
format(date: Date, displayFormat: Object): string {
if (displayFormat == "input") {
return moment(date).format(dateFormat);
} else {
return date.toDateString();
}
}
}
export const MY_FORMATS = {
parse: {
dateInput: { day: 'numeric',month: 'short', year: 'numeric' }
},
display: {
dateInput: 'input',
monthYearLabel: { day: 'numeric', month: 'short', year: 'numeric' },
dateA11yLabel: { day: 'numeric', month: 'long', year: 'numeric' },
monthYearA11yLabel: { year: 'numeric', month: 'long' },
}
};
请有人帮帮我吗?如何格式化日期以填充我的日期选择器字段?
根据doc,行为取决于您使用的适配器。由于数据库返回的非标准字符串格式,因此无法在您的情况下使用NativeDateAdapter
。你应该尝试用MomentDateAdapter
代替。