[date_time] Get age in years

I'm trying to get the age of someone in Years and/or months and/or days. I've tried: date_period dp(day_clock::local_day(), birthdate); cout << "You are " << dp.length() << " old" << endl; But that only calculates days. Is there a static function available that will turn days into months. and days into years? Thanks -Jason

Jason Dolan wrote:
I'm trying to get the age of someone in Years and/or months and/or days.
I've tried:
date_period dp(day_clock::local_day(), birthdate); cout << "You are " << dp.length() << " old" << endl;
But that only calculates days. Is there a static function available that will turn days into months. and days into years?
I don't know the answer to that question, but it would have to be a function of not only the age in days but also the birth date. A person born on 1 January 2003 is 365 days old on their first birthday, while someone born on 1 January 2004 is 366 days old on their first birthday. On their first birthdays, each of them is a year old, but the second person is a day older than the first. So simply dividing by 365 and using the remainder for the days in the part-year would not be sufficient.

Paul Giaccone
A person born on 1 January 2003 is 365 days old on their first birthday, while someone born on 1 January 2004 is 366 days old on their first birthday. On their first birthdays, each of them is a year old, but the second person is a day older than the first. So simply dividing by 365 and using the remainder for the days in the part-year would not be sufficient.
There is a perl module called Date::Manip that has some ideas on how to deal with date differences. You can browse the documentation at cpan.org. Look for the DateCalc function.

Jason Dolan wrote:
I'm trying to get the age of someone in Years and/or months and/or days.
I've tried:
date_period dp(day_clock::local_day(), birthdate); cout << "You are " << dp.length() << " old" << endl;
But that only calculates days. Is there a static function available that will turn days into months. and days into years?
Why don't you use date::year(), date::month() and date::day() and substract the values instead of date_period? Boris

Boris wrote:
Jason Dolan wrote:
I'm trying to get the age of someone in Years and/or months and/or days.
I've tried:
date_period dp(day_clock::local_day(), birthdate); cout << "You are " << dp.length() << " old" << endl;
But that only calculates days. Is there a static function available that will turn days into months. and days into years?
Why don't you use date::year(), date::month() and date::day() and substract the values instead of date_period?
Boris
I ended up doing something like that. You have to keep track of leap years however.
participants (4)
-
Boris
-
Jason Dolan
-
Paul Giaccone
-
Peter Whaite