You are on page 1of 11

1

How to calculate duration of time using crystal reports?

How do you create a formula in Crystal Reports (CR) that adds more than
one time field and displays the total in the same format: hh:mm:ss?

For example:
1:45:01 + 1:45:01 should display 03:30:02 and not 2:90:02

Solution
In order to get the sum of more than one time field and to display the
total of the time field in the same format (hh:mm:ss) you must complete
the following steps:

A. Convert all the time fields to a common time unit such as seconds
B. Calculate the total time in seconds
C. Convert the seconds back to hh:mm:ss format.

A.-Convert time fields to seconds


--------------------------------
Convert the time fields to seconds so that a common time unit is used to
sum up all three different time units.

If your field is a datetime field, complete the following steps:


1. Create a new formula and call it @ConvertTimeToSeconds
2. To convert the datetime field to seconds, create a formula similar to
the following:
Code:
local numbervar hours;
local numbervar minutes;
local numbervar seconds;

// Convert the hours to seconds by multiplying by 3600


hours := hour({@time}) * 3600;

// Convert the minutes to seconds by multiplying by 60


minutes := minute({@time}) * 60;
seconds := second({@time});

//add up all the seconds


hours + minutes + seconds;

If your field is string field with the format of hh:mm:ss to convert to


seconds complete the following steps:
1. Create a new formula and call it @ConvertTimeStringToSeconds
2. Create a formula similar to the following to convert the string field
to seconds:
Code:
local numbervar hours;
local numbervar minutes;
local numbervar seconds;

// Parse out the hours portion of the string and multiply by 3600 to convert to seconds
hours := tonumber({timestringfield}[1 to 2])* 3600;

// Parse out the minutes portion of the string and multiply by 60 to convert to seconds
minutes := tonumber({timestringfield}[4 to 5]) * 60;

// Parse out the seconds


seconds := tonumber({timestringfield}[7 to 8]);

// Add up all the seconds


hours + minutes + seconds;

B.-Calculate the total time in seconds


------------------------------------
Create a summary formula that will sum the
@ConvertTimeToSeconds or @ConvertTimeStringToSeconds
1. Create a new formula and call it @TotalSeconds
2. To sum up either formula, create a formula similar to the following:
[Code]
sum(@ConvertTimeToSeconds, Group)
- OR -
sum(@ConvertTimeStringToSeconds, Group)

C.-Convert the seconds back to hh:mm:ss format.


--------------------------------------------
Create a formula that converts the @TotalSeconds results back to
hh:mm:ss format:
1. Create a new formula and call it @ConvertTotalSeconds
2. To convert the results from @TotalSeconds back to hh:mm:ss format,
create a formula similar to the following:
Code:
local numbervar RemainingSeconds;
local numbervar Hours ;
local numbervar Minutes;
local numbervar Seconds;

//divide the @TotalSeconds by 3600 to calculate hours.


//Use truncate to remove the decimal portion.
Hours := truncate({@TotalSeconds} / 3600);

// Subtract the hours portion to get RemainingSeconds


RemainingSeconds := {@TotalSeconds} - (Hours * / 3600);

// Divide RemainingSeconds by 60 to get minutes.


//Use truncate to remove the decimal portion.
Minutes := truncate(RemainingSeconds/60);

// Subtract the Hours and Minutes and what is left over is seconds.
Seconds := {@Totalseconds} - (Hours * 3600) - (Minutes * 60);

// Format the hours, minutes, and seconds to hh:mm:ss


totext(Hours,"00") + ":" + totext(Minutes,"00") + ":" + totext(Seconds,"00")

2
Reset the variable in header:
Global numbervar x;
x:=0;

Now write your formula in formula filed and place as a last field in detail
section:

Global numbervar x;
x:=x+databasefield;

Now in page footer write below:

Global numbervar x;
x
3
Pt numerotare randuri:

1st Formula: HeaderReset - This formula should be placed in the page header
of the report. It will reset the counter when the report is processed to the next
page. Counter is a variable that stores the number as the report processes
each record.

WhilePrintingRecords;
NumberVar counter:=0

2nd Formula: CountDetails - This formula should be placed in the Details


section. It will count each record and increment by one.

WhilePrintingRecords;
NumberVar counter;
counter:=counter+1

you then can use that in a conditional surpression or a formula

if record count >= 20 then ......


4
Count & Reset based on Crystal Report Rows

Are the grey bands sections like a group header?


You could add a variable and use it to count.

In the group header set it to 0.


WhilePrintingRecords;
Global NumberVar Counter;
Counter := 0'
""
Select all
Open in new window
IN the detail section increment it then use it to display the value as :

WhilePrintingRecords;
Global NumberVar Counter;
Counter := Counter + 1;
"Part " & ToText(Counter),0)

https://www.mindstick.com/Articles/614/running-total-field-in-crystal-
report
5
Converting total seconds in the format of DD:HH:MM:SS:
Converting total seconds to a formatted "elapsed time" string:
When you need to display an elapsed time in the format of DD:HH:MM:SS, you
can convert the time value to a total seconds number and use the following
formula to convert the seconds total to a formatted string. All you need to do
is replace the dummy field on the end of the second line with your net seconds
or total seconds field. I am using WhilePrintingRecords but you can use
WhileReadingRecords in cases where the {YourTable.TotalSeconds} field does
not involve a Crystal Summary operation like Sum() or Average().
WhilePrintingRecords;
NumberVar TotalSec := {YourTable.TotalSeconds};
NumberVar Days := Truncate (TotalSec / 86400);
NumberVar Hours := Truncate (Remainder ( TotalSec , 86400) / 3600) ;
NumberVar Minutes := Truncate (Remainder ( TotalSec , 3600) / 60) ;
NumberVar Seconds := Remainder (TotalSec , 60) ;

Totext ( Days , '##' ) + ':' +


Totext ( Hours , '00' ) + ':' +
Totext ( Minutes , '00' ) + ':' +
Totext ( Seconds , '00' )

If you want to display only in hours and minutes you should use this version:
WhilePrintingRecords;
NumberVar TotalSec := {YourTable.TotalSeconds};

NumberVar Hours := Truncate ( TotalSec / 3600);


NumberVar Minutes := Truncate (Remainder ( TotalSec,3600) / 60);

Totext ( Hours, '####') + ':'+


Totext ( Minutes,'00')
6
Pt rotunjire Timp:
For example: I have this 02:03:49 (hh:mm) i would like it rounded to 02:00:00
or 02:09:49 (hh:mm) rounded to 02:15:00
or 02:33:49 (hh:mm) rounded to 02:30:00
or 02:46:49 (hh:mm) rounded to 02:45:00
or 02:51:27 (hh:mm) rounded to 03:00:00

formula:
//for display the time to the closest quarter hour
local timeVar ti:={DB.Field};//database field(if it is date field use
time({DB.Field}))
numberVar min:= Minute(ti)/15;
numberVar RoundMin:=Truncate(min+0.5)*15;
time(hour({DB.Field}),RoundMin,00)
sau :

////Use the revised formula in the following formula:


//whileprintingrecords;
//numberVar dur := {@your formula};
//numberVar hrs;
//numberVar min;
//numberVar sec;
//stringVar hhmmss;
//
//hrs := Truncate(Truncate(dur/60)/60);
//min := Remainder(Truncate(dur/60),60);
//if min in 01 to 15 then min := 15 else
//if min in 16 to 30 then min := 30 else
//if min in 31 to 45 then min := 45 else
//if min in 46 to 59 then
//(min := 00;
//hrs := hrs + 1) else
//min := min;
//sec := Remainder(dur,60);
//hhmmss := totext(hrs, "0") + ":" + totext(min, "00") + ":" + totext(sec, "00");
//hhmmss

Sau:

Ex: daca vreau sa rotunjesc 07:31:42 la 08:00:00 (adica peste 30 min sa fie 1
ora)

whileprintingrecords;
numberVar dur := {@DateDiff};
numberVar hrs;
numberVar min;
numberVar sec;
stringVar hhmmss;

hrs := Truncate(Truncate(dur/60)/60);
min := Remainder(Truncate(dur/60),60);
if min in 01 to 29 then min := 00 else
if min in 30 to 59 then
(min := 00;
hrs := hrs + 1) else
min := min;
hhmmss := totext(hrs, "0") + ":" + totext(min, "00") + ":" + totext(sec, "00");
hhmmss
7
Eliminare zile weekend si concediu:
/Main formula:

WhileReadingRecords;
Local DateVar Start := {StartDate}; // place your Starting Date here
Local DateVar End := {EndDate}; // place your Ending Date here
Local NumberVar Weeks;
Local NumberVar Days;
Local Numbervar Hol;
DateVar Array Holidays;
Weeks:= (Truncate (End - dayofWeek(End) + 1
- (Start - dayofWeek(Start) + 1)) /7 ) * 5;
Days := DayOfWeek(End) - DayOfWeek(Start) + 1 +
(if DayOfWeek(Start) = 1 then -1 else 0) +
(if DayOfWeek(End) = 7 then -1 else 0);
Local NumberVar i;
For i := 1 to Count (Holidays)
do (if DayOfWeek ( Holidays[i] ) in 2 to 6 and
Holidays[i] in start to end then Hol:=Hol+1 );
Weeks + Days – Hol

https://kenhamady.com/formulas/default.html
8
Introducere luna in pagina dupa un parametru

if Month ({?Start}) = 1 then "Ianuarie"


else if Month ({?Start}) = 2 then "Februarie"
else if Month ({?Start}) = 3 then "Martie"
else if Month ({?Start}) = 4 then "Aprilie"
else if Month ({?Start}) = 5 then "Mai"
else if Month ({?Start}) = 6 then "Iunie"
else if Month ({?Start}) = 7 then "Iulie"
else if Month ({?Start}) = 8 then "August"
else if Month ({?Start}) = 9 then "Septembrie"
else if Month ({?Start}) = 10 then "Octombrie"
else if Month ({?Start}) = 11 then "Noiembrie"
else if Month ({?Start}) = 12 then "Decembrie"

9
Conversie timp (format “number”) in timp

Ctime ({time number field} /86400) => “time” format

10
Conversie “number ” la “string”

Ex:
Number totext({number}) totext({number},0,””)
 108 108,00 108

You might also like