Click here to Skip to main content
15,887,241 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
I have the total numbers of hours in the string format(HH:MM:SS) in dataset.I want to calculate sum of HH:MM:SS in crystal Report.

For example 52:42:04 +20:34:04 = 73:16:08
I tried the below code in function...But the Output is 00:00:00

What I have tried:

WhilePrintingRecords; 
NumberVar TotalSec :=  tonumber({TimeUsedReport.TotalHours});
NumberVar Hours   := Truncate  (Remainder ( TotalSec , 86400) / 3600) ; 
NumberVar Minutes := Truncate  (Remainder ( TotalSec , 3600) / 60) ; 
NumberVar Seconds := Remainder (TotalSec , 60) ;


Totext ( Hours ,   '00' ) +  ':' + 
Totext ( Minutes , '00' ) +  ':' + 
Totext ( Seconds , '00' ) 
Posted
Updated 17-May-17 0:51am
v2
Comments
Richard Deeming 17-May-17 13:05pm    
This is yet another example of why you should never store dates and times as strings.

After all, it's not like SQL doesn't have specific types[^] for storing them.

1 solution

You may try this -

create a formula in the details that will convert the HH:MI:SS in seconds.Lets call it @tot_seconds.

write this code in the formula -

local stringvar array completetime;
local numbervar totalseconds;
completetime:=split({@combinetime},":");
totalseconds:= (3600*cdbl(completetime[1])) + (60*cdbl(completetime[2]))+(cdbl(completetime[3]));

This formula will return the total seconds for every record shown in the details section.substitute your field with the @combinetime.

Summarize this field ,add a total for the above formula in the reports footer section.Now you have the total time in seconds.

we will convert back this time to the HH:MI:SS.Create a formula and put in report footer.Write the following code in the formula -

replace(cstr(floor(Sum ({@tot_seconds})/3600)),".00","")
+ ":" + replace(cstr(floor((Sum ({@tot_seconds}) mod 3600)/60)),".00","")
+ ":" + replace(cstr(floor(Sum ({@tot_seconds}) mod 60)),".00","")

The result of this formula will show the total time in the HH:MI:SS format.

Source: The ASP.NET Forums

Quote:
Please, mark it as the solution if it helps.
 
Share this answer
 
Comments
Developer29 18-May-17 0:24am    
It's throwing error field is required...
Zunayed Shahriar 18-May-17 1:32am    
See, the source link... You'll get the full conversation.
Developer29 18-May-17 1:43am    
Thanks....it's working now....
Zunayed Shahriar 18-May-17 2:32am    
You're most welcome

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900