SAP Knowledge Base Article - Public

1561336 - How to calculate the difference between two Time fields and display it in hours, minutes and seconds in Crystal Reports?

Symptom

  • How to calculate the difference in time between 2 time fields?
  • Difference between two time fields display the result in seconds.
  • How to calculate the difference between two Time fields in Crystal Reports, and display the result in hours, minutes and seconds?

Environment

  • SAP Crystal Reports 2011
  • SAP Crystal Reports 2013
  • SAP Crystal Reports 2016
  • SAP Crystal Reports 2020

Reproducing the Issue

  1. In Crystal Reports, create a report against any data source which has multiple fields with data type ‘time’
         
    For example:
      
    If there are two columns:
    - StartTime 
    - EndTime
      
    We would like to find the difference between these two columns.
         
  2. Create a new formula called ‘Time Difference’ like:
          
       {EndTime} - {StartTime}
           
  3. Save the above formula.
        
  4. Place the database fields for startTime and endTime along with the formula that we created above in the details section and refresh the report.
       
    The report will display the difference between the start time and end time in seconds, how do we display the time difference in hours, minutes, and seconds?

Resolution

  • To display the time in hours, minutes and seconds between two fields that contains time, add the difference in seconds to the function "Time" at midnight:
          
    1. In Crystal Reports, create or open a report.
        
    2. Create a formula to calculate the time difference, using a code like:
         
      TimeVar StartTime := <INSERT SART TIME FIELD HERE>;
      TimeVar EndTime := <INSERT END TIME FIELD HERE>;
      Time(0,0,0) + (EndTime - StartTime);
          
    3. Insert the formula on the report.

Keywords

Calculate time difference, Crystal Reports , 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