2

could you please tell me how to calculate the age of person using moment js ? I have two dates "DOB" and "current date" .i want to get difference between them to get age .

Age like 15 year , 14 year ..

here is my code http://plnkr.co/edit/1wIvVISmgEqcRNnAD971?p=preview

let DOB= "09-Feb-1983"
console.log(moment(DOB, 'DD-MMM-YYYY', true).isValid())
console.log(moment().format('DD-MMM-YYYY'));
user944513
  • 12,247
  • 49
  • 168
  • 318
  • 1
    Possible duplicate of [Moment.js - how do I get the number of years since a date, not rounded up?](https://stackoverflow.com/questions/14057497/moment-js-how-do-i-get-the-number-of-years-since-a-date-not-rounded-up) – VincenzoC Nov 14 '18 at 08:39

2 Answers2

12

Try this

moment().diff('09-Feb-1983', 'years');

Ramesh Rajendran
  • 37,412
  • 45
  • 153
  • 234
1

Found this site https://www.sitepoint.com/managing-dates-times-using-moment-js/ on the first result of Google search

var dateB = moment('2010-11-11');
var dateC = moment('2014-10-11');

console.log('Difference is ', dateB.diff(dateC), 'milliseconds');
console.log('Difference is ', dateB.diff(dateC, 'days'), 'days');
console.log('Difference is ', dateB.diff(dateC, 'months'), 'months');
console.log('Difference is ', dateB.diff(dateC, 'years'), 'years');
Ahmad
  • 12,336
  • 6
  • 48
  • 88