Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

sum a string??? 1

Status
Not open for further replies.

richardtm

IS-IT--Management
Jan 7, 2005
12
0
0
US
I've created a formula that shows the elapsed time between a startdate and stopdate in the format of HH:MM (see below). It works great. Now I need to sum these string fields in HH:MM format. Any ideas??? I don't even know where to begin. Thanks for any help you may have.

Matt

@showelapsedtime
//This is the formula name

DateTimeVar dt1:= {Beginning or Start Datetime
Field};
DateTimeVar dt2:= {Ending or Stop Datetime Field};

If dt2 >= dt1 Then
(
NumberVar ds:= (Date(dt2) - Date(dt1))*86400;
// ds converts the difference in days, between
// the two DateTimes, to seconds
NumberVar hs:= (Hour(dt2) - Hour(dt1))*3600;
// hs converts the difference in hours to seconds
NumberVar ms:= (Minute(dt2) - Minute(dt1))*60;
// ms converts the difference in minutes to seconds
Numbervar ss:= Second(dt2) - Second(dt1);
NumberVar ts:= ds+hs+ms+ss;
// ts adds up the total difference in seconds
// between the two DateTime fields
ds:= Truncate(ts/86400);
// ds now finds the number of days in the total
// seconds difference
hs:= Truncate((Remainder(ts,86400))/3600);
// hs now finds the number of hours (ensuring
// that hs is between 0 and 23)
ms:= Truncate((Remainder(ts,3600))/60);
// ms now finds the number of minutes (ensuring
// that ms is between 0 and 59)
ss:= Truncate(Remainder(ts,60));
StringVar display:= ToText(ds,0,"") + ":" +
ToText(hs,0,"") + ":" + ToText(ms,0,"") + ":" +
ToText(ss,0,"")
// display will be the screen output of the formula
// in a text format DD:HH:MM:SS
)

Else
// this portion is used only when the beginning
// DateTime is greater than the Ending DateTime
(
NumberVar ds:= (Date(dt1) - Date(dt2))*86400;
NumberVar hs:= (Hour(dt1) - Hour(dt2))*3600;
NumberVar ms:= (minute(dt1) - Minute(dt2))*60;
NumberVar ss:= Second(dt1) - Second(dt2);
NumberVar ts:= ds+hs+ms+ss;

ds:= Truncate(ts/86400);
hs:= Truncate((Remainder(ts,86400))/3600);
ms:= Truncate((Remainder(ts,3600))/60);
ss:= Truncate(Remainder(ts,60));
StringVar display:= "-" + ToText(ds,0,"") + ":" +
ToText(hs,0,"") + ":" + ToText(ms,0,"") + ":" +
ToText(ss,0,"")
);
 
By the way, this is Crystal Reports 8.5
 
See faq767-3543. What you need to do is sum the elapsed time in seconds before converting it to a string for display purposes.

-LB
 
Rather tehn writing ur own logic u should use crystal report inbuilt function
following code will solve ur prob.

datetimevar dt1:=CurrentDateTime; //begin date
datetimevar dt2:=CurrentDateTime; //end date
numbervar mnts;
numbervar hrs;
mnts:=DateDiff ("n",dt1 ,dt2 ); //return diffence in mnts
hrs:=mnts/60; //get complete hrs
mnts:=remainder(mnts,60); // get remaiing minutes
totext(hrs)+':'+totext(mnts); // output as string HH:MM



 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top