tags:

views:

566

answers:

5

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!

+3  A: 

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
RJFalconer
Language agnostic and also quite inaccurate...
stakx
er... counter example please?
RJFalconer
@RJFalconer: I didn't mean to be disrespectful; my apologies for the above comment if it seems offensive. I actually didn't see the first line, saying _"for just the year"_, and therefore commented because your method only turns out the year difference and not -- as was asked -- months and days, too. My bad, and sorry again if any offense was given.
stakx
np, thanks for the follow up =). I think I'll be more careful posting interesitng-but-not-exactly-relevant answers in future. 1 down vote and one cancelled upvote =(.
RJFalconer
+2  A: 

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.

duffymo
+4  A: 

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.

Uri
+1, but I disagree about the assumption. Easy enough to hard code days in a month neatly in a case statement with fall through, and algorithm for determining leap years is simple to find; http://en.wikipedia.org/wiki/Leap_year#Algorithm
RJFalconer
@RJFalconer: You're probably right. But my experience is that at very intro level C courses they usually don't try to trip students with "end cases" like leap years. The focus is more on correct input and integer divisions.
Uri
I don't think that doing the calculation by determining the difference-in-days and then converting that is the best way to do this - it totally glosses over the difficulty of the "break it back into years/months/days" step. For example, a difference of 30 days might mean any of "0 months, 30 days", "1 month, 2 days", "1 month, 1 day" or "1 month, 0 days", depending on what the dates actually are.
caf
A: 

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).

Matthew Flaschen
A: 

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.

caf