dob format is 2022-07
desired output is 0 Years 5 Months
Below is the code I tried but I am getting months in negative.
export default function calculateAge(date) {
let month = new Date().getMonth() - Number(date.split("-")[1]);
let year = new Date().getFullYear() - Number(date.split("-")[0]);
console.log(`month is`, month);
if (month < 0 && year < 1) {
month = year * 12 + month;
year = 0;
}
console.log(`year`, year);
return `${year ? `${year} Year${year > 1 ? `s` : ""}` : ""} ${
month ? `${month} Month${month > 1 ? "s" : ""}` : ""
}`;
}