dhayden wrote: |
---|
Like I said, this is harder than it appears. |
It isn't really. You just have to map out the logic for it. This is less of a programming challenge and more of a logic challenge -- but the logic is something OP should know how to do. His hangup is probably that he's trying to write the code before he figured out what he wants his code to actually do.
Let's use your example:
cur date: 2014/8/7
birth date: 2013/8/15
What is the age? Before even thinking about how you'd code it...
do the problem by hand.
I did it by hand and came up with:
0 years, 11 months, 23 days
When you think about it... it's just a problem of subtraction. Subtract cur_date - birth_date. Start with days, and work down towards the years.
days
cur = 7
birth = 15
7 - 15 = -8 (can't have negative days, so borrow 31 days from the previous month)
-8 + 31 =
23 days
months
cur = 8-1 = 7 (borrowed 1 to add 31 days)
birth = 8
7 - 8 = -1 (can't have negative months, so borrow 12 months from the previous year)
-1 + 12 =
11 months
years
cur = 2014 - 1 = 2013 (borrowed 1 to add 12 months)
birth = 2013
2013 - 2013 =
0 years
You just have to work out the logic.