SAP Knowledge Base Article - Public

2892268 - How to verify if a date falls in a leap year in Crystal Reports?

Symptom

  • How to check if it is a leap year?
  • How to verify if a date falls within a leap year in Crystal Reports?  

Environment

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

Resolution

  • To verify if a date falls within a leap year, create a formula:
        
    1. In Crystal Reports, open the report on which you have a database field you want to verify if it falls within a leap year.
         
    2. Create a formula and enter the following code:      

      Local DateVar myDate := <INSERT THE DATE FIELD HERE> 
      Local NumberVar myYear := Year(myDate);
        
      If isDate(ToText(myYear,0,"")+",2,29") Then
          "Leap Year"
      Else
          "Regular Calendar Year"    

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

    4. Save and close the formula.

    5. Insert the formula on the report where you want to display if it is a leap year.
         
           
            
  • Bonus: To generate a list of leap years from 1900, create a formula like:  

         Local StringVar Array aLeapYears;
         Local NumberVar myYear;
         Local NumberVar x := 0;

         For myYear := 1900 to 2100 Do
         (
              If isDate(ToText(myYear,0,"")+",2,29") Then
              (
                   x := x + 1;
                   Redim Preserve aLeapYears[x];
                   aLeapYears[x] := ToText(myYear,0,"");
              );
         );
       
         Join(aLeapYears,chr(13))

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