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
Calculating age given DOB-PIC maths
Moderator: Benj
-
- Posts: 594
- Joined: Thu Sep 17, 2009 7:52 am
- Location: Belgium
- Has thanked: 63 times
- Been thanked: 102 times
- Contact:
Re: Calculating age given DOB-PIC maths
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:
BR,
Nicolas
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
}
}
Nicolas