-1

I want to display age of all users in my meanjs app. How can i display age instead of displaying birthdate. my plunk demo

Controller:

$scope.agedate = new Date();
   $scope.calculateAge = function calculateAge(birthday) { 
    var ageDifMs = Date.now() - birthday.getTime();
    var ageDate = new Date(ageDifMs); // miliseconds from epoch
    return Math.abs(ageDate.getUTCFullYear() - 1970);
}

Html:

<p ng-bind="items.user.displayName"></p>
<p ng-bind="items.user.dateofbirth | date"></p>
<p ng-bind="calculateAge(items.user.dateofbirth)"></p>

my data:-

$scope.items = {
"_id": "5733163d4fc4b31d0ff2cb07",
"user": {
"_id": "5732f3954fc4b31d0ff2cb05",
"displayName": "karthi keyan",
"dateofbirth": "1991-10-04T18:30:00.000Z",
"profileImageURL": "./modules/users/client/img/profile/uploads/ed948b7bcd1dea2d7086a92d27367170"
},
"__v": 0,
"comments": [],
"content": "this is testing purpose for e21designs",
"categoryone": "Moral Ethics",
"category": "Anonymous Question",
"title": "Worried",
"created": "2016-05-11T11:23:41.500Z",
"isCurrentUserOwner": true
};

My plunk demo

R. Mani Selvam
  • 320
  • 8
  • 36
  • Possible duplicate of [Calculate age in JavaScript](http://stackoverflow.com/questions/4060004/calculate-age-in-javascript) – Heretic Monkey May 11 '16 at 16:24

3 Answers3

0

Your code almost does what you want.

It has a problem in dateofbirth property, because it's a string (according your example.

To display it as the date you're using date filter which handles this for you.

But, in your calculateAge function you need to convert your string into Date.

Try the following:

$scope.calculateAge = function calculateAge(birthday) { // birthday is a string
    var ageDifMs = Date.now() - new Date(birthday).getTime(); // parse string to date
    var ageDate = new Date(ageDifMs); // miliseconds from epoch
    return Math.abs(ageDate.getUTCFullYear() - 1970);
}

Hope it will help.

MaKCbIMKo
  • 2,800
  • 1
  • 19
  • 27
0

Please note that this problem is completely unrelated to angularjs. It is pure Javascript date differences calculation. I strongly suggest to use a third party library like (momentjs)[http://momentjs.com/] to make such calculation, and in order to help you parse the string formatted date.

Hamdi Douss
  • 1,033
  • 1
  • 8
  • 17
0

Here is a simple function in javascript to calculate age for the date format "YYYY-MM-DD". Where the dateString parameter to the function is the birth date.

function calculateAge(dateString) {
  var today = new Date();
  var birthDate = new Date(dateString);
  var age = today.getFullYear() - birthDate.getFullYear();
  var m = today.getMonth() - birthDate.getMonth();
  if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
      age--;
  }
  return age;
}

You could use this as an angular function by applying $scope to it. Like this:

$scope.calculateAge = function(dateString) {
  var today = new Date();
  var birthDate = new Date(dateString);
  var age = today.getFullYear() - birthDate.getFullYear();
  var m = today.getMonth() - birthDate.getMonth();
  if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
      age--;
  }
  return age;
}
Mohit Yadav
  • 156
  • 1
  • 8