Bilal Dekar
Bilal Dekar

Reputation: 3986

How to format Date in inputText

I'm using primeng, how can i display a date in input with another format (dd/MM/yyyy) :

<label class="col-form-label" >Date</label>
<input type="text" size="30" pInputText [(ngModel)]="object.dateCreation" [readonly]="true" >

object: Object = {
    dateCreation: new Date();
}

Below how the date is diplayed :

enter image description here

Upvotes: 0

Views: 1324

Answers (2)

Akhil
Akhil

Reputation: 1453

You can use Angular DatePipe

For displaying in HTML ::

{{dateCreation | date: 'dd/MM/yyyy'}}

For sending data to backend, Inject its dependency such as

providers :[DatePipe]

constructor(private datePipe:DatePipe) {}

and now, transform the date using the method available :

this.datePipe.transform(this.dateCreation, 'dd/MM/yyyy');

Upvotes: 1

behruz
behruz

Reputation: 598

you should apply pipe this way:

<input type="text" size="30" pInputText [ngModel]="object.dateCreation | date : 'medium" (ngModelChange)="object.dateCreation = $event" [readonly]="true">

Upvotes: 2

Related Questions