Convert birthday to angularjs

I want to show the age of all my users in the grid. I am reading data from facebook. I do not store it anywhere.

I am showing a date like:

{{ friend.birthday }} 

How can I display age instead of displaying a birthday.

if you can create filters, how to create a filter and how to apply it.

+7
angularjs angularjs-filter
source share
4 answers

You can implement the function:

Controller:

 $scope.calculateAge = 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); } 

HTML

 {{ calculateAge(friend.birthday) }} 

Or filter:

 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); } return function(birthdate) { return calculateAge(birthdate); }; }); 

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

+28
source share

If you are standing, for example, "05/01/2016". This will be a useful code for converting a date to a birthday.

Angularjs

 app.filter('ageFilter', function(){ return function(birthday){ var birthday = new Date(birthday); var today = new Date(); var age = ((today - birthday) / (31557600000)); var age = Math.floor( age ); return age; } }); 

HTML

 {{ relationTypePreDefined.birthdate | ageFilter }} 

By the way, I used this solution to convert the date coming from jqery datepicker date input to age.

+2
source share

If you are using momentjs. Then you can create a filter just using this snippet

 var now = "04/09/2013 15:00:00"; var then = "04/09/2013 14:20:30"; moment.utc(moment(now,"DD/MM/YYYY HH:mm:ss").diff(moment(then,"DD/MM/YYYY HH:mm:ss"))).format("HH:mm:ss") 
+1
source share

Idk, why I can never answer people, says that I need more reputation, but I need to give a comment.

In response to @Dean Christian Armada, I kept getting an error regarding the filter. But moving on to the next seems to work just fine, so I appreciate that!

 $scope.getAge = function(birthday){ var birthday = new Date(birthday); var today = new Date(); var age = ((today - birthday) / (31557600000)); var age = Math.floor( age ); return age; } 

And for HMTL

 {{ getAge(birthdate) }} 
0
source share

All Articles