Given a date of birth, how would I go about calculating an age in C?
For example, if today's date is 20/04/2010 and the date of birth given is 12/08/86, then age will be 23 years, 8 months, and 8 days.
Any suggestions would be appreciated. Thanks!
Given a date of birth, how would I go about calculating an age in C?
For example, if today's date is 20/04/2010 and the date of birth given is 12/08/86, then age will be 23 years, 8 months, and 8 days.
Any suggestions would be appreciated. Thanks!
For just the year (no months/days) :
1) Format the dates to yyyymmdd
2) Subtract the date of birth from date
3) Remove the last 4 numbers
(Language agnostic)
So for your example;
date 20/04/2010
birth 12/08/1986
convert
date 20100420
birth 19860812
subtract
20100420-19860812 = 239608
drop last 4 digits
23
If today's date falls after the birthday, the age is the difference between the birth year and today's year. If today's date falls before the birthday, subtract 1.
I'm assuming, based on your description, that you have to print out the full difference, not just years.
One part of the problem is to input the dates correctly and break them down into components, I'll assume you already know how to do that or got sample code for that.
What you need to do now is to calculate the difference. One way to do this would be to pick a reference date (e.g., Jan 1st 1900), and calculate how many days it had been until the first date and the second date, and calculate the difference in days. Then you take the difference in days and break it back into years/months/days.
Two things to notice are:
1) Take leap years into account.
2) Figure out how to translate a number of dates into months, since each month has a different number of days.
3) If you had to input times and not just dates, you could be off by a day. Similarly if time zones are input.
I would check with the instructor whether you can make a simplifying assumption about that (E.g., months are always 30, leap years are ignored, etc.). I find it hard to believe that a homework assignment would require you to deal with these correctly.
If you're allowed to use the libraries, it's not too hard. Look at strptime, struct tm, time, and localtime. Once you have it in "broken-down" form (struct tm
), it's easy to compute the difference (look at tm_yday
, tm_mon
, and tm_yday
).
The way to approach a problem like this is to figure out how you'd do it using a pencil and paper - then formalise that into a program.
For this particular problem, that means at a high level, "subtract birth date from current date". For this subtraction, you use a variation of the same algorithm you learn for subtraction in primary school - where you start by subtracting the lower value column (in this case, "days"), borrowing from the next-higher-value column if necessary. For example, to subtract 1986-09-15 from 2010-04-10, you would do:
2010-04-10 -
1986-09-15
----------
10 is less than 15, so you have to borrow from the months column. This means that the months column goes down by one (to 3), and the days column goes up by the number of days in month 3 (March - so 31). You can now do the subtraction of the days column:
2010-03-41 -
1986-09-15
----------
-26
We can now move on to the months column. 3 is less than 9, so we have to borrow again, this time from the year. Take one off the year, and add 12 to the month (since there are always 12 months in a year), then perform the subtraction:
2009-15-41 -
1986-09-15
----------
-06-26
We can now work on the years - there's never a need to borrow here (unless you're trying to calculate the age of someone born in the future!):
2009-15-41 -
1986-09-15
----------
23-06-26
This means the difference is 23 years, 6 months, 26 days.
You can now work on turning this into a program (hint: use three seperate integer variables for years, months and days). The trickiest part is the borrow from the months column - you need to know how many days are in that month, including leap years for February.