How to use of Angular Pipes

Pies is used for the transform the data when display. It takes data as input it transform your desire output to display.
Syntax

{{datevalue | date:"MM/dd/yy" }}

In above syntax ” | ” operator called pipe operator then after we can pass the pipe. In Angular few pipes already created its called Built in Pipes. We can create custom pipe also its called Custom Pipe.


Built in Pipes
Angular built in pipes such as DatePipe, UpperCasePipe, LowerCasePipe, CurrencyPipe, and PercentPipe.
All pipes available for use in any template.

How to DatePipe Uses in Template

date_value | date[:format[:timezone[:locale]]]

“date_value” its a date object or its timestamp in millisecond.
“Format” : Predefined format available for in Angular.

  • ‘short’: display to ‘M/d/yy, h:mm a’ (e.g. 12/23/2017, 7:10 PM)
  • ‘medium’: display to ‘MMM d, y, h:mm:ss a’ (e.g. Dec 23, 2017, 7:10:01 PM)
  • ‘long’: display to ‘MMMM d, y, h:mm:ss a z’ (e.g. December 23, 2017 at 7:10:01 PM GMT+1)
  • ‘full’: display to ‘EEEE, MMMM d, y, h:mm:ss a zzzz’ (e.g. Saturday , December 23, 2017 at 7:10:01 PM GMT+01:00)
  • ‘shortDate’: display to ‘M/d/yy’ (e.g. 12/23/2017)
  • ‘mediumDate’: display to ‘MMM d, y’ (e.g. Dec 23, 2017)
  • ‘longDate’: display to ‘MMMM d, y’ (e.g. December 23, 2017)
  • ‘fullDate’: equivalent to ‘EEEE, MMMM d, y’ (e.g. Saturday , December 23, 2017)
  • ‘shortTime’: display to ‘h:mm a’ (e.g. 10:10 AM)
  • ‘mediumTime’: display to ‘h:mm:ss a’ (e.g. 10:10:01 AM)
  • ‘longTime’: display to ‘h:mm:ss a z’ (e.g. 11:08:01 AM GMT+1)
  • ‘fullTime’: display to ‘h:mm:ss a zzzz’ (e.g. 10:03:01 AM GMT+01:00)



Timezone to be used for Formatted time particular timezone. for that we need to pass the offset like “+0530” it means (5 hours 30 min Kolkata Timezone ). If we didn’t pass then it takes default browser timezone.

Blow are the examples of the DatePipe pipe how to use and output of it.


@Component({
  selector: 'date-pipe',
  template: `
<div>
<!-- Output : Mar 11, 2018 --> 
Today is {{today | date}} 
<!-- Output: Sunday , March 11, 2018 --> 
Full Date {{today | date:'fullDate'}} 
<!-- Output: 2018-03-11 09:03 AM GMT+0530--> 
Timezone date is {{fixedTimezone | date:'yyyy-MM-dd HH:mm a z':'+0530'}}
</div>
` }) 
export class DatePipeComponent { 
today = Date.now(); 
fixedTimezone = '2018-03-11T09:03:01+0530'; 
}

Next article i can discuss on other builtin pipes uses in Template.

Spread the love
  • 24
  •  
  •  
  •  
  •  
  •  
  •  
  •