phone: (540)338-0194
email: ken@kenhamady.com

´╗┐ PDF exPLODE

 Crystal Reports formula examples

Calculating an accurate Age:

I was surprised that this formula needed to be written.   The problem with calculating age is that it is calculated as an integer, and always rounded down.  If you are 20 years and 364 days old, you are still considered 20. The next day you gain one whole year in age.   You could simply subtract date of birth from current date and divide by 365.25.  The result is pretty accurate as long you are showing some decimal places and not rounding up.   But to get a more precise age Integer you can use the formula below. Note that by default it increments that age of a 'leap-year baby' on 2/29 in leap-years but on 3/1 in years that are NOT leap years.  There are some countries (Taiwan, New Zealand, etc) that specify that this happens on 2/28 in non-leap years.  If you need to follow that rule you can activate the section that is commented out by removing the slashes on those 5 lines.  
WhileReadingRecords;
DateVar Birth:= {ages.Birth};   // Replace this with your field for Date Of Birth
DateVar Ann := {ages.DateAnn};  // Replace this with CurrentDate to get their age at runtime
                                                            //or a date field to get their age as of that date

//To increment the age of 'leap-year' babies on 2/28 instead of 3/1 (in non-leap years)

//if (Month(Ann) * 100) + Day (Ann) = 228
//and (Month(Ann+1) * 100) + Day (Ann+1) = 301
//and (Month(Birth) *100) + Day (Birth) =229
//then Year (Ann) - Year(Birth)
//else

if (Month(Ann) * 100) + Day (Ann) >=(Month(Birth) *100) + Day (Birth)
then Year (Ann) - Year(Birth)
else Year (Ann) - Year(Birth) -1