SAP Knowledge Base Article - Public

1538006 - How to calculate a person’s age in Crystal Reports?

Symptom

  • How to calculate the age of a person?
  • How to calculate a person’s age on a particular date in Crystal Reports?

Environment

  • SAP Crystal Reports 2011
  • SAP Crystal Reports 2013
  • SAP Crystal Reports 2016
  • SAP Crystal Reports 2020
                
  • SAP Crystal Reports for Enterprise 4.0
  • SAP Crystal Reports for Enterprise 4.1
  • SAP Crystal Reports for Enterprise 4.2
  • SAP Crystal Reports for Enterprise 4.3

Resolution

  • To calculate the age of a person as of today, create a formula.
       
    1. In Crystal Reports, open the report on which you have a database field that contains birthday.
           
    2. Create a formula and enter the following code:        

      DateVar birthday := <INSERT THE BIRTHDAY FIELD HERE>;
      DateVar vToday := Today;
          
      // Verify if the birthday was on a leap year, and if the current year is a leap year.
      If Month(birthday) = 2 and Day(birthday) = 29 Then
         If not isDate(ToText(Year(vToday),0,"")+",2,29") Then
            birthday := birthday + 1;
         
      // Calculate the age
      DateDiff('yyyy',birthday,vtoday) - (If Date(Year(vToday),Month(Birthday),Day(Birthday)) > vToday Then 1 Else 0)

    3. Insert the date field in the formula where it is written: <INSERT THE BIRTHDAY FIELD HERE>

    4. Save and close the formula.

    5. Insert the formula on the report where you want to display the age.

Keywords

CR , KBA , BI-RA-CR , Crystal Reports designer or Business View Manager , How To

Product

SAP Crystal Reports 2011 ; SAP Crystal Reports 2013 ; SAP Crystal Reports 2016 ; SAP Crystal Reports 2020