How to use angular2 datipip in input

I have a date string coming from my server as below:

1992-05-26T18:30:00Z

I am trying to format this in DD / MM / YYYY format using date protocol and bind it to my ngModel input.

 var userdate:any = new Date(data.draftData.accountHolder.dateOfBirth); this.setDob = userdate | date:'MM/DD/YYYY'; 

It gives me an error message:

ReferenceError: date not defined

My HTML is as follows

 <md-input placeholder="Date of birth" value="mm/dd/yy" [(ngModel)]="setDob"></md-input> 

Can someone point out what I'm missing here.

+1
source share
2 answers

It is assumed that pipes should be used in the template, not in the code.

 <div>{{userdate | date:'MM/DD/YYYY'}}</div> 

You can also use it in code, for example

 var userdate:any = new Date(data.draftData.accountHolder.dateOfBirth); var datePipe = new DatePipe(); this.setDob = datePipe.transform(userdate, 'MM/DD/YYYY'); 
+4
source
 let datepipe = new DatePipe('en-US'); let date = new Date(data.draftData.accountHolder.dateOfBirth); let formattedDate = this.datepipe.transform(date,"MM/dd/y HH:MM"); 
-1
source

Source: https://habr.com/ru/post/1259937/


All Articles