function calculateDateOfBirth(ageYears, ageMonths = 0) {
const currentDate = new Date();
const birthYear = currentDate.getFullYear() - ageYears;
const birthMonth = currentDate.getMonth() - ageMonths;
let birthDate = currentDate.getDate();
if (birthMonth < 0 || (birthMonth === 0 && birthDate < 0)) {
birthYear--;
birthMonth += 12;
}
if (birthDate < 0) {
const monthDays = new Date(
currentDate.getFullYear(),
currentDate.getMonth(),
0
).getDate();
birthMonth--;
birthDate += monthDays;
}
const dateOfBirth = new Date(birthYear, birthMonth, birthDate);
return dateOfBirth.toISOString().split('T')[0];
}
const ageYears = 30;
const ageMonths = 6;
const dateOfBirth = calculateDateOfBirth(ageYears, ageMonths);
console.log(dateOfBirth);