如何在angular 2的[ngModel]中格式化dateFormat?

时间:2019-06-18 07:29:53

标签: angular date ionic-framework date-formatting angular-date-format

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

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

3 个答案:

答案 0 :(得分:1)

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

<p> {{WarrentItem.dateOfRegistartion | date: 'dd/MM/YYYY'}} </p>

答案 1 :(得分:1)

您可以在[ngModel]中使用remove()

<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

答案 2 :(得分:0)

尝试这样

方法1

<input [ngModel]="startDate | date:'yyyy-MM-dd'" (ngModelChange)="startDate = $event" type="date" name="startDate"/>

方法2:

模板

<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);
  }
}