JSFiddle - React, Tailwind, and code Playground
JavaScript
function getAge(dateString) {
var calendarArray = [];
calendarArray[0] = "31";
calendarArray[1] = "28";
calendarArray[2] = "31";
calendarArray[3] = "30";
calendarArray[4] = "31";
calendarArray[5] = "30";
calendarArray[6] = "31";
calendarArray[7] = "31";
calendarArray[8] = "30";
calendarArray[9] = "31";
calendarArray[10] = "30";
calendarArray[11] = "31";
var today = new Date();
var birthDate = new Date(dateString);
var age = today.getFullYear() - birthDate.getFullYear();
var m = today.getMonth() - birthDate.getMonth();
var d = today.getDate() - birthDate.getDate();
var dobAddYear = birthDate.getFullYear() + 1;
if (dobAddYear < today.getFullYear() || age > 1) {
if (m < 0 || (m === 0 && today.getDate() < birthDate.getDate())) {
age--;
document.write(age + 'Y');
return;
}
document.write(age + 'Y');
} else {
if (age === 0 && today.getDate() >= birthDate.getDate()) age = m + "M";
if (age === 0 && today.getDate() < birthDate.getDate()) age = (m - 1) + "M";
//document.write("Print" + age);
if(age === 1) {
var calMon = 12 - (birthDate.getMonth() - (today.getMonth()));
if(calMon > 12) {
document.write(age + 'Y');
} else {
document.write(12 - (birthDate.getMonth() - (today.getMonth() -1)) + 'M');
}
return;
}
if (m === 0 && today.getDate() - birthDate.getDate()) {
age = d + "D";
}
if (m === 0 && today.getDate() === birthDate.getDate()) {
age = d + "D";
}
if (m === 1) {
var previousMonth = calendarArray[today.getMonth() - 1];
age = today.getDate() + (previousMonth - birthDate.getDate())
if (age > calendarArray[today.getMonth() - 1]) {
document.write(m + 'M');
...