1

如何在 [ngModel] 中像 dd-MM-YYYY 这样格式化日期?

<input type="text" class="displayInfo"  disabled ngDefaultControl [(ngModel)]="WarrentItem.dateOfRegistartion">
4

3 回答 3

1

您可以直接使用日期管道以特定格式显示日期

<p> {{WarrentItem.dateOfRegistartion | date: 'dd/MM/YYYY'}} </p>
于 2019-06-18T07:35:37.700 回答
1

您可以在中使用 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

于 2019-06-18T07:39:07.990 回答
0

试试这样

方法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);
  }
}
于 2019-06-18T07:43:30.337 回答