JSFiddle - React, Tailwind, and code Playground
JavaScript
var dob = "13-05-1981";
mdob = moment(dob, 'DD-MM-YYYY'); // date of birth
if (!mdob.isValid()) {
alert("Invalid date format");
return;
}
targetDate = moment(); //this will give today's date
months = targetDate.diff(mdob, 'months');
let years = parseInt(months / 12);
let balanceMonths = months % 12;
let days;
if (!balanceMonths) {
months = 0;
// days = targetDate.diff(mdob, 'days');
dob_date = mdob.date();
target_month = targetDate.month();
construct_date = moment().month(target_month).date(dob_date);
days = targetDate.diff(construct_date, 'days');
if(days < 0){
days = 30 + days;
}
} else {
months = balanceMonths;
dob_date = mdob.date();
target_month = targetDate.month();
construct_date = moment().month(target_month).date(dob_date);
days = targetDate.diff(construct_date, 'days');
if(days < 0){
days = 30 + days;
}
}
console.log("months", months)
console.log("years", years)
console.log("days", days)