Angular - Use pipes in services and components

前端 未结 8 1450
南笙
南笙 2020-11-22 09:42

In AngularJS, I am able to use filters (pipes) inside of services and controllers using syntax similar to this:

$filter(\'date\')(myDate, \'yyyy-MM-dd\');
         


        
8条回答
  •  忘了有多久
    2020-11-22 10:14

    You can use formatDate() to format the date in services or component ts. syntax:-

    formatDate(value: string | number | Date, format: string, locale: string, timezone?: string): string
    

    import the formatDate() from common module like this,

    import { formatDate } from '@angular/common';
    

    and just use it in the class like this ,

    formatDate(new Date(), 'MMMM dd yyyy', 'en');
    

    You can also use the predefined format options provided by angular like this ,

    formatDate(new Date(), 'shortDate', 'en');
    

    You can see all other predefined format options here ,

    https://angular.io/api/common/DatePipe

提交回复
热议问题