The problem is a lot harder than I thought.
Here is a temporary solution. Whether the solution gets completed by me is a matter of my free time. Feel free to modify the code as I really have only done the minimal.
You may want to:
Add month lengths to correctly calc days,
Account for Leap years,
Change the output to include plurals (Yrs, not Yr) <-- I'm too lazy
Apart from that, I think the code should work.
Here is a possibility
Code:
TargetDate = TargetDate.split(" ");
TargetDate = TargetDate[0].split("/") //Target Date in array format where: TargetDate[0] = month, TargetDate[1] = day(number), TargetDate[2] = year
CurrentDate = new Date().toString().split(" "); //Date in array format where: CurrentDate[0] = Day(name), CurrentDate[1] = Month(name)..... day(number), year, time, etc etc
//Convert month name to month number
Month_Array = new Array("Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Oct","Nov","Dec");
var Pos = 0;
var month_number = -1;
while(month_number == -1)
{
if(Month_Array[Pos]==CurrentDate[1])
{
month_number = Pos;
if(Pos>12)
{
break;
}
}
Pos++
}
//Calculate age
yearsOld = CurrentDate[3]-TargetDate[2];
monthsOld = (month_number>parseInt(TargetDate[0]))? month_number-parseInt(TargetDate[0]) : (12-parseInt(TargetDate[0]))+month_number;
daysOld = (parseInt(CurrentDate[2])>parseInt(TargetDate[1]))? parseInt(CurrentDate[2])-parseInt(TargetDate[1]) : (31-parseInt(TargetDate[1]))+parseInt(CurrentDate[2]);
//Take your pick from below or make your own
age_in_days = (yearsOld*365) + (monthsOld*30) + daysOld;
age_in_days_and_months = (yearsOld*12) + (monthsOld) + "Months " + daysOld +"Days";
age_full_with_abbr_units = yearsOld+"Yr " + monthsOld+"Mn " + daysOld+"Dy";
Bookmarks