You can implement the function:
Controller:
$scope.calculateAge = function calculateAge(birthday) {
HTML
{{ calculateAge(friend.birthday) }}
Or filter:
app.filter('ageFilter', function() { function calculateAge(birthday) {
HTML
{{ friend.birthday | ageFilter }}
Age-based algorithm taken from this SO answer .
[EDIT] If the age is less than 1 year and you want to show months, you can change ageFilter to calculate the difference in the month:
app.filter('ageFilter', function() { function calculateAge(birthday) { // birthday is a date var ageDifMs = Date.now() - birthday.getTime(); var ageDate = new Date(ageDifMs); // miliseconds from epoch return Math.abs(ageDate.getUTCFullYear() - 1970); } function monthDiff(d1, d2) { if (d1 < d2){ var months = d2.getMonth() - d1.getMonth(); return months <= 0 ? 0 : months; } return 0; } return function(birthdate) { var age = calculateAge(birthdate); if (age == 0) return monthDiff(birthdate, new Date()) + ' months'; return age; }; });
Demo Plunker - age function
Demo Plunker - Filter by age
Demo Plunker - filter of age with months <1 year
pixelbits
source share