Technical Questions
 Crystal Reports Forum : Crystal Reports 9 through 2022 : Technical Questions
Message Icon Topic: Calculating time value Post Reply Post New Topic
Author Message
PJsFort
Newbie
Newbie
Avatar

Joined: 29 Apr 2009
Location: United States
Online Status: Offline
Posts: 6
Quote PJsFort Replybullet Topic: Calculating time value
     Posted: 29 Apr 2009 at 3:53pm
I need help or direction.  I'm fairly new at Crystal Reports and need help with a formula issue.
 
Situation: Access & Excel are my sources.  I need to calculate the difference between (2) string values that need a result of a Time value in hours, minutes, seconds. 
 
I've tried converting the source to number fields, but they still show up as strings in Crystal.   I've tried several things, but can't seem to come up with the correct formula or combination of formulas that work.  Any suggestions would be great!
 
Thanks.
PJ
PJs Fortress
IP IP Logged
DBlank
Moderator
Moderator


Joined: 19 Dec 2008
Online Status: Offline
Posts: 9053
Quote DBlank Replybullet Posted: 29 Apr 2009 at 9:05pm
what do the two strings look like as text in crystal?
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 30 Apr 2009 at 6:17am
well, if they aren't exporting in as numbers, but as strings, as long as they look like times, things should be ok.  I am going on the assumption that the strings look like times.
 
since you want the difference, something like this should work...
time({table.fieldendtime}) - time({table.fieldstarttime})
 
If they don't look like time strings, let's know, and we will see what we can do.
IP IP Logged
PJsFort
Newbie
Newbie
Avatar

Joined: 29 Apr 2009
Location: United States
Online Status: Offline
Posts: 6
Quote PJsFort Replybullet Posted: 30 Apr 2009 at 8:15am
Thank you for your response.  
 
The field does look like time when inserted into the Crystal report (69:34:15).   The field is actual time logged into a phone, hours, minutes,seconds and I need to calculate quarterly stats,  minus the downtime minutes.  
 
When I used the following formula  ({'Team_Stats_'.Logged_In}) - time({'Team_Stats_'.Not_Ready})  I get a Bad Time format string error.
 
The datasource field type is text.  I've tried converting to number and setting the time string.   I'm missing something in formula.   
 
Here is an example of one thing I tried.
 
WhilereadingRecords;
StringVar Hours1;
StringVar Minutes1;
StringVar Seconds1;
numberVar agentlogtime1:=0;

agentlogtime1:= {@Not ready time numeric};
If agentlogtime1 < 0 Then
  "Cannot have a time less than zero"
Else
(
  (Hours1:=Totext(Truncate(agentlogtime1/3600),0);
  Minutes1:=ToText(Truncate(Remainder(agentlogtime1,3600)/60),0);
  Seconds1:=ToText(Remainder(Remainder(agentlogtime1,3600),60),0));
//Display the time formated.
  (if length(Hours1) < 2 then '0') + Hours1 + ":" +
  ["0",""][length(Minutes1)] + Minutes1 + ":" +
  ["0",""][length(Seconds1)] + Seconds1;
)
 
the reference formula that equals agentlogtime1 = If NumericText ({ACD_Data.Logged_In}) Then
     ToNumber ({ACD_Data.Logged_In})
Else
     0
 
This all results in 00:00:00. 
 
A lot of information here.  Hope you can make sense of it and have a suggestion. 
 
PJsFort
PJs Fortress
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: 01 May 2009 at 1:29am
Hi
 
Search in forums
 
How can I calculate the difference between 2 times ,
 
I have done for Hours and Minutes since you have seconds in your Time part you will need to add the code to get seconds part
 
here is the post
 

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
 


Edited by rahulwalawalkar - 01 May 2009 at 1:32am
IP IP Logged
lockwelle
Moderator
Moderator


Joined: 21 Dec 2007
Online Status: Offline
Posts: 4374
Quote lockwelle Replybullet Posted: 01 May 2009 at 6:23am
I was thinking something like:
 
local timevar startTime := time({table.startTimeColumn})
 
then use what Rahul has for the conversion using the variable, or at the very least see what results you get.  If it saves typing, I am all for trying.
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