SAP Knowledge Base Article - Public

1310861 - How to convert seconds to days, hours, minutes and seconds, in Crystal Reports?

Symptom

  • How to convert seconds to days, minutes, and hours?
  • How to take number of seconds, and it display it in days, minutes and hours in Crystal Reports?

Environment

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

Resolution

  • To display the the number of seconds in days, hours, minutes and seconds, create a formula:  
            
    1. In Crystal Reports, create a report based on any data source that contains a fields returning a number of seconds.
            
    2. Create a formula to convert the number of seconds, and display it in days, hours, minutes and seconds:   

      Local NumberVar Seconds := <INSERT YOUR FIELD CONTAINING NUMBER OF SECONDS HERE>;
      Local NumberVar Days;
      Local NumberVar Hours;
      Local NumberVar Minutes;

      Days := Truncate( Seconds / 86400,0 );
      Seconds := Seconds - ( Days * 86400 );
      Hours := Truncate( Seconds / 3600,0 );
      Seconds := Seconds - ( Hours * 3600 );
      Minutes := Truncate( Seconds / 60,0 );
      Seconds := Seconds - ( Minutes * 60 );

      ToText(Days,0) + ' Day(s) ' + ToText(Time(Hours,Minutes,Seconds), "HH:mm:ss");
        

    3. Insert the formula on the report.
       
            
    • Note:
      The result can be displayed in different ways. Below is another formula example showing the duration in Days, Hours, Minutes and Seconds, in the format: X Days, X Hours, X Minutes, X Seconds 
         
      Local NumberVar Seconds := <INSERT YOUR FIELD CONTAINING NUMBER OF SECONDS HERE>;
      Local NumberVar Days;
      Local NumberVar Hours;
      Local NumberVar Minutes;

      Days := Truncate( Seconds / 86400,0 );
      Seconds := Seconds - ( Days * 86400 );
      Hours := Truncate( Seconds / 3600,0 );
      Seconds := Seconds - ( Hours * 3600 );
      Minutes := Truncate( Seconds / 60,0 );
      Seconds := Seconds - ( Minutes * 60 );

      Local StringVar Output;
      Output := If Days > 0 Then ToText(Days,0) + (If Days = 1 Then ' Day ' Else ' Days ');
      Output := Output + (If Hours > 0 Then ToText(Hours,0) + (If Hours = 1 Then ' Hour ' Else ' Hours '));
      Output := Output + (If Minutes > 0 Then ToText(Minutes,0) + (If Minutes = 1 Then ' Minute ' Else ' Minutes '));
      Output := Output + (If Seconds > 0 Then ToText(Seconds,0) + (If Seconds = 1 Then ' Second ' Else ' Seconds '));

Keywords

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

Product

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