Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: How can I calculate the difference between 2 times Post Reply Post New Topic
Author Message
wafaa
Newbie
Newbie


Joined: 21 Sep 2008
Location: Kuwait
Online Status: Offline
Posts: 18
Quote wafaa Replybullet Topic: How can I calculate the difference between 2 times
     Posted: 03 Dec 2008 at 11:16pm
Dear All,
 
How can I calculate the difference between 2 times in hours and minutes only?
eg.
Start Time           End Time           Time Diff
 
08:00                    09:30                01:30
 
Regards
IP IP Logged
rahulwalawalkar
Senior Member
Senior Member
Avatar

Joined: 08 Jun 2007
Location: United Kingdom
Online Status: Offline
Posts: 731
Quote rahulwalawalkar Replybullet Posted: 04 Dec 2008 at 5:58am

Hi
This is what you do create the formulas below,

I have shown you for start time repeat the same steps for End Time

Frm_StartTime_Hrs
if LEFT({Parent_Child.FinishTime},2)= '0' or LEFT({Table.StartTime},2)= ' '
then
0.00
else
tonumber(LEFT({Table.StartTime},2))

Frm_StartTime_Mins
if Right({Table.StartTime},2)= '0' or Right({Table.StartTime},2)= ' '
then
0.00
else
tonumber(Right({Table.StartTime},2))

Once done

Create another set of formulas to convert the time to seconds

Convert StartTimeHoursMins
local numbervar hours;
local numbervar minutes;

// Parse out the hours portion of the string and
// multiply by 3600 to convert to seconds
hours := {@Frm_Frm_StartTime_Hrs}* 3600;
// Parse out the minutes portion of the string and
// multiply by 60 to convert to seconds
minutes := {@Frm_Frm_StartTime_Mins} * 60;
// Add up all  seconds
hours + minutes

this will give you output in seconds

Repeat the above steps for End Time

 

Once done Subtract the

Convert EndTimeHoursMins from Convert StartTimeHoursMins call this formula as diff_time

you will get the output in seconds use the final formula

Frm_Final
local numbervar RemainingSeconds;
local numbervar Hours ;
local numbervar Minutes;


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

// Subtract the hours portion to get RemainingSeconds
RemainingSeconds := {@diff_time} - (Hours *3600);

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

// Format the hours, minutes, and seconds to hh:mm:ss
totext(Hours,"00") + ":" + totext(Minutes,"00")

Cheers
Rahul
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