How to use Pipe in Component?

Forums AngularHow to use Pipe in Component?
Staff asked 8 months ago

I want to use DatePipe in component in angular project.

Answers (1)

Add Answer
monika gabani Marked As Accepted
Staff answered 8 months ago

DatePipe is use to set format for date in angular.

To use datePipe first you need to import it.

import { DatePipe } from ‘@angular/common’;

class AppComponent {

constructor(private datePipe: DatePipe) {}
date:any = new Date();
transformDate(date) {
return this.datePipe.transform(date, ‘yyyy-MM-dd’);
}
this.transformDate(this.date);
}

you can also use inbuilt pipe date format for date like in html file

<h2>{{date}}</h2>
<h2>{{date | date:’short’}}</h2>
<h2>{{date | date:’shortDate’}}</h2>
<h2>{{date | date:’shortTime’}}</h2>

Thanks.

Subscribe

Select Categories