Page 1 of 1

Calculating age given DOB-PIC maths

Posted: Sat Jul 02, 2011 4:21 pm
by nmindana
Hi,

I am trying to formulate an age calculator, where in if you input your date of birth, it outputs your age in days.
Can anybody help me with some ideas or a simple programme ?. :(

many thanks

Indana

Re: Calculating age given DOB-PIC maths

Posted: Sat Jul 02, 2011 9:29 pm
by Spanish_dude
It's not that difficult to do, assuming the PIC knows which day, month and year we are "today".

I'd probably do something like this:

Code: Select all

char age, month, day, input_month, input_day;
short year, input_year;

// year, month and day is given by a RTC (?)
// input_year, input_month and input_day is given by the user

age = year - input_year - 1;

if ((month - input_month) > 0)     // if (month - input_month) is higher than 0
{
    age = age + 1; // add 1 to age
}
else if (month == input_month) // if the given month is equal to the month given by the RTC
{ 
    if ((day - input_day) >= 0) // and if (day - input_day) is higher or equal to 0
    {
        age = age + 1; // add 1 to age
    }
}
BR,

Nicolas