Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: calculatre time difference for each group Post Reply Post New Topic
Author Message
maas
Newbie
Newbie


Joined: 26 May 2009
Location: Bahrain
Online Status: Offline
Posts: 15
Quote maas Replybullet Topic: calculatre time difference for each group
     Posted: 26 Jan 2011 at 8:40pm
Hello All,

I am facing difficulties while developing this report and your help is appreciated.
My report details section looks like this:
employee No.    Time                                  Type     

123

                           1-Jan-2011 7:00:01AM    In 
                           1-Jan-2011 2:15:00 PM   Out
 
So, how do I get a summary at the end for each employee the time that he spent in the office. (2:15:00 PM - 7:00:00 AM) to get 7 hours and 15 minutes in the summary at the group footer.

Also, If the employee enters before 7 o'clock, let the calculation start from 7 not from the date which he entered.

So, how do I implement this logic?

Thanks for your help


Edited by maas - 26 Jan 2011 at 8:41pm
IP IP Logged
FrnhtGLI
Senior Member
Senior Member
Avatar

Joined: 22 May 2009
Online Status: Offline
Posts: 347
Quote FrnhtGLI Replybullet Posted: 27 Jan 2011 at 2:42am
You would need to use the DateDiff function. I have a report that calculates this similarly. I'm assuming the Time field is just one field. I created two fields {Login} and {Logout}. {Login} is just {table.timefield} and {Logout} is next{table.timefield}. Then create a field called {TotalSeconds} that is datediff('s', {Login},{Logout}). This will give you the total seconds. You will then have to convert that into a time. I used this formula:
 
whileprintingrecords;
NumberVar Hours   := Truncate (Remainder ({TotalSeconds} , 86400) / 3600);
NumberVar Minutes := Truncate (Remainder ({TotalSeconds} , 3600) / 60);
NumberVar Seconds := Remainder ({TotalSeconds}, 60) ;
 
Hours & ':' & Minutes & ':' & Seconds;
 
Okay, so maybe there is some easier way, but this got me the results I needed. Hope it at least helps some.
|< /\ '][' ( )
IP IP Logged
maas
Newbie
Newbie


Joined: 26 May 2009
Location: Bahrain
Online Status: Offline
Posts: 15
Quote maas Replybullet Posted: 27 Jan 2011 at 4:56am
Thanks my friend for the help.
 
But, an issue came is that one employee might have more than one "In" and "Out" in the day. So, how to sum these durations? Or what do you suggest, so at the end of each group footer there will be Total Hours and the result. So, how do I do that?
IP IP Logged
FrnhtGLI
Senior Member
Senior Member
Avatar

Joined: 22 May 2009
Online Status: Offline
Posts: 347
Quote FrnhtGLI Replybullet Posted: 27 Jan 2011 at 9:48am
I guess you could keep a manual running total of the seconds based on the Type field. Maybe something like:
 
whileprintingrecords;
 
global numbervar nTotalSeconds:=nTotalSeconds + (if {table.type}='In' and next({table.type})='Out'
     then datediff('s' , {Login},{Logout})
          else 0);
nTotalSeconds;
 
You'll need to create a formula to reset the running total in the Employee Group Header: nTotalSeconds=0;
|< /\ '][' ( )
IP IP Logged
maas
Newbie
Newbie


Joined: 26 May 2009
Location: Bahrain
Online Status: Offline
Posts: 15
Quote maas Replybullet Posted: 27 Jan 2011 at 10:12am
This will show the result in seconds.
 
And then I have to convert it to hours and minutes (HH:MM:SS)?
IP IP Logged
FrnhtGLI
Senior Member
Senior Member
Avatar

Joined: 22 May 2009
Online Status: Offline
Posts: 347
Quote FrnhtGLI Replybullet Posted: 28 Jan 2011 at 3:09am
with the formula in my first post:
 
whileprintingrecords;
NumberVar Hours   := Truncate (Remainder ({TotalSeconds} , 86400) / 3600);
NumberVar Minutes := Truncate (Remainder ({TotalSeconds} , 3600) / 60);
NumberVar Seconds := Remainder ({TotalSeconds}, 60) ;
 
Hours & ':' & Minutes & ':' & Seconds;
|< /\ '][' ( )
IP IP Logged
maas
Newbie
Newbie


Joined: 26 May 2009
Location: Bahrain
Online Status: Offline
Posts: 15
Quote maas Replybullet Posted: 28 Jan 2011 at 10:01pm
Thanks for the reply.
 
I will try this and return back
 
IP IP Logged
Printable version Printable version

Forum Jump
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot delete your posts in this forum
You cannot edit your posts in this forum
You cannot create polls in this forum
You cannot vote in polls in this forum