1

How to format date like dd-MM-YYYY in [ngModel] ?

<input type="text" class="displayInfo"  disabled ngDefaultControl [(ngModel)]="WarrentItem.dateOfRegistartion">
lealceldeiro
  • 13,596
  • 5
  • 44
  • 76
Zeljk0
  • 51
  • 6

3 Answers3

1

you can use date pipe directly to display the date with particular format

<p> {{WarrentItem.dateOfRegistartion | date: 'dd/MM/YYYY'}} </p>
Jatin Devani
  • 204
  • 2
  • 7
1

You can use remove () in [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

Hien Nguyen
  • 23,011
  • 7
  • 48
  • 55
  • its ok , but i've got today's Date, i dont need that. I need to get a date from mssql in this format. – Zeljk0 Jun 18 '19 at 07:41
0

try like this

Method1

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

Method 2:

Template

<input [ngModel]="humanDate" type="date" name="startDate"/>

Component (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);
  }
}
Jaykant
  • 360
  • 1
  • 9