Link to home
Start Free TrialLog in
Avatar of Mark Wilson
Mark Wilson

asked on

Converting seconds into days, hours and minutes

I have a field in my report which is in seconds

I want to convert in into working days, hours and minutes

For example

33600 = 1 day 2 hrs 20 mins
50400 = 2 days
7200 = 2 hours
900 = 15 mins etc

There are 25200 seconds in our working days

Any help would be appreciated
Avatar of Tomislavj
Tomislavj
Flag of Croatia image

can this script help?
Avatar of Mark Wilson
Mark Wilson

ASKER

Thanks, it can to a certain extent.

i.e. for example I can get 50400 to equal 2 days 0 hrs and 0 mins or 7200 to equal 0 days 2 hrs and 0 mins, its getting rid of the zeros

i.e. just get the example1 to equal 2days and 2 hrs for eample 2
select

cast(sum(datediff(ss,a.date_time_in, isnull(a.date_time_out,getdate()) )) / 86400 as varchar(4)) + ' Days '+

right('0'+cast((sum(datediff(ss,a.date_time_in, isnull(a.date_time_out,getdate()) )) % 86400)/3600 as varchar(4)),2) +' Hours ' +

right('0'+cast((sum(datediff(ss,a.date_time_in, isnull(a.date_time_out,getdate()) )) % 86400) % 3600)/60 as varchar(4)),2) +' Minutes ' +

right('0'+cast((sum(datediff(ss,a.date_time_in, isnull(a.date_time_out,getdate()) )) % 86400) %3600)%60 as varchar(4)),2) + ' Seconds'
ASKER CERTIFIED SOLUTION
Avatar of Mike McCracken
Mike McCracken

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
mlmcc's formula, with some corrections and tweaks:

WhilePrintingRecords;
NumberVar TotalSec :=  {seconds field};
NumberVar Days    := Truncate  (TotalSec / 25200);
NumberVar Hours   := Truncate  (Remainder ( TotalSec , 25200) / 3600) ;
NumberVar Minutes := Truncate  (Remainder ( TotalSec , 3600) / 60) ;
NumberVar Seconds := Remainder (TotalSec , 60) ;
Local StringVar strOut;

If Days > 0 then
    strOut := ToText (Days, "#") + " day" + IIF (Days > 1, "s ", " ");

If Hours > 0 then
    strOut := strout & ToText (Hours , "#") & " hour" + IIF (Hours > 1, "s ", " ");

if Minutes > 0 then
    strout := strout & ToText (Minutes , "#") & " minute" + IIF (Minutes > 1, "s ", " ");

If Seconds > 0 then
    strout := ToText (Seconds, "#") + " second" + IIF (Seconds > 1, "s ", " ");

strout


 I added a space after the hour count, changed the ToText formats to remove some leading spaces and zeros, removed the unnecessary strOut = "" tests, changed the seconds per day from 86400 to 25200, changed the units (days, hours, etc.) to be singular if the count was 1, etc.

 James