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 derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Seconds to days, hours, minutes

Status
Not open for further replies.

jeffm777

IS-IT--Management
Nov 10, 2009
108
US
I have a numeric field {Warehouse.Duration} that contains seconds. I need a formula to convert this field to show days, hours, and minutes. Here's what I have thus far:
Code:
//{@Duration_Calc}
WhilePrintingRecords; 
NumberVar TotalSec :=  {Warehouse.Duration}; 
NumberVar Days    := Truncate  (TotalSec / 86400); 
NumberVar Hours   := Truncate  (Remainder ( TotalSec , 86400) / 3600) ; 
NumberVar Minutes := Truncate  (Remainder ( TotalSec , 3600) / 60) ; 

Totext ( Days ,    '##' ) +  ' Days, ' + 
Totext ( Hours ,   '##' ) +  ' Hrs, and ' + 
Totext ( Minutes , '##' ) +  ' Mins'
The above formula returns the following which works, but it's not clean.

{Warehouse.Duration} = 418680
{@Duration_Calc} = 4 Days, 20 Hrs, and 18 Mins

{Warehouse.Duration} = 4920
{@Duration_Calc} = 0 Days, 1 Hrs, and 22 Mins

{Warehouse.Duration} = 60
{@Duration_Calc} = 0 Days, 0 Hrs, and 1 Mins

------------------------------------------------------------

What I would like the formula to return is this.

{Warehouse.Duration} = 418680
{@Duration_Calc} = 4 Days, 20 Hrs, and 18 Mins

{Warehouse.Duration} = 4920
{@Duration_Calc} = 1 Hr, 22 Mins

{Warehouse.Duration} = 60
{@Duration_Calc} = 1 Min

 
You need an if for Days and hours to check if they are zero and not include them in the string.

stringvar finalstr;
if Days <> 0 then finalstr := finalstr & Totext ( Days , '##' ) + ' Days, '
if Hours <> then finalstr := finalstr & Totext ( Hours , '##' ) + ' Hrs, and '
finalstr := finalstr & Totext ( Minutes , '##' ) + ' Mins'

Of course the calulations for the Days, Hours and Minutes will need to be included. There may be some fomatting anomolies if you have days but not hours.
 
I would replace your this part of the formula:
Totext ( Days , '##' ) + ' Days, ' +
Totext ( Hours , '##' ) + ' Hrs, and ' +
Totext ( Minutes , '##' ) + ' Mins'

with below.

stringvar dlbl;
IF Days=0 then dlbl:=" " else dlbl := "Days, ";
stringvar hlbl;
IF Days=0 and Hours=0 then hlbl:=" " else hlbl :="Hrs, and ";

TRIM(totext(Days & dlbl & Hours & hblb & Minutes & "Min"))

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top