如何在 [ngModel] 中像 dd-MM-YYYY 这样格式化日期?
<input type="text" class="displayInfo" disabled ngDefaultControl [(ngModel)]="WarrentItem.dateOfRegistartion">
如何在 [ngModel] 中像 dd-MM-YYYY 这样格式化日期?
<input type="text" class="displayInfo" disabled ngDefaultControl [(ngModel)]="WarrentItem.dateOfRegistartion">
您可以直接使用日期管道以特定格式显示日期
<p> {{WarrentItem.dateOfRegistartion | date: 'dd/MM/YYYY'}} </p>
您可以在中使用 remove()[ngModel]
<input type="text" class="displayInfo" disabled ngDefaultControl [ngModel]="dateOfRegistartion | date: 'dd/MM/yyyy'">
https://stackblitz.com/edit/angular-ztqvfy?file=src%2Fapp%2Fapp.component.html
试试这样
方法1
<input [ngModel]="startDate | date:'yyyy-MM-dd'" (ngModelChange)="startDate = $event" type="date" name="startDate"/>
方法二:
模板
<input [ngModel]="humanDate" type="date" name="startDate"/>
组件(TS):
export class App {
startDate: any;
constructor() {
this.startDate = new Date(2005, 1, 4);
}
set humanDate(e){
e = e.split('-');
let d = new Date(Date.UTC(e[0], e[1]-1, e[2]));
this.startDate.setFullYear(d.getUTCFullYear(), d.getUTCMonth(), d.getUTCDate());
}
get humanDate(){
return this.startDate.toISOString().substring(0, 10);
}
}