import { Pipe , PipeTransform } from '@angular/core' ; @ Pipe ( { name : 'age' } ) export class AgePipe implements PipeTransform { transform ( birthDate ) { return calcAge ( birthDate ) ; } } export function calcAge ( birthDate ) { if ( birthDate == null ) { return null ; } const date = birthDate instanceof Date ? birthDate : new Date ( birthDate ) ; if ( isNaN ( date . getFullYear ( ) ) ) { return null ; } else { const ageDifMs = Date . now ( ) - date . getTime ( ) ; const ageDate = new Date ( ageDifMs ) ; return Math . abs ( ageDate . getUTCFullYear ( ) - 1970 ) ; } }