Solved

elapsed seconds as hours:minutes:seconds

Posted on 2003-11-11
7
754 Views
Last Modified: 2010-04-05
I have an integer variable with a total number of elapsed seconds stored in it. I need to format it as  hours:minutes:seconds

can someone create a function for me which takes an integer of elapsed seconds and returns a string formatted as such : hours:minutes:seconds


Thanks

Shane

0
Comment
Question by:shaneholmes
7 Comments
 
LVL 6

Expert Comment

by:DaFox
Comment Utility
Hi Shane,

Are you looking for sth. like this?

procedure TForm1.Button1Click(Sender: TObject);

  function ConvertSecondsToTime(iSeconds: Word): TDateTime;
  var
    hr, min: Word;
  begin
    hr := trunc(iSeconds / 3600);
    iSeconds := iSeconds - (3600 * trunc(iSeconds / 3600));
    min := trunc(iSeconds / 60);
    iSeconds := iSeconds - (60 * trunc(iSeconds / 60));
    Result := encodeTime(hr, min, iSeconds, 0);
  end;

begin
  ShowMessage(TimeToStr(ConvertSecondsToTime(42343)));
end;

This (nested) function converts seconds to TDateTime format. The date is incorrect, of course, but you're able to extract the time like shown in my example. You can also change Word to LongWord if you want to...

Markus
0
 
LVL 2

Accepted Solution

by:
Trekker72 earned 50 total points
Comment Utility
Hi,

this will give you elapsed time as a string, but it needs tidying up...

function GetTimeStr(const TimeInSecs: Integer): string;
var
  iHours, iMinutes, iSecs: Integer;
  sHours, sMinutes, sSecs: string;
begin
  iHours := Floor(TimeInSecs / 3600);
  sHours := IntToStr(iHours);
  Result := IfThen(iHours > 0, StringOfChar('0', 2 - Length(sHours)) +
    sHours, '00') + ':';
  iMinutes := Floor((TimeInSecs mod 3600) / 60);
  sMinutes := IntToStr(iMinutes);
  Result := Result + IfThen(iMinutes > 0,
    StringOfChar('0', 2 - Length(sMinutes)) + sMinutes, '00') + ':';
  iSecs := Floor((TimeInSecs mod 3600) mod 60);
  sSecs := IntToStr(iSecs);
  Result := Result + IfThen(iSecs > 0,
    StringOfChar('0', 2 - Length(sSecs)) + sSecs, '00');
end;

btw it uses Math and StrUtils units
0
 
LVL 11

Author Comment

by:shaneholmes
Comment Utility
I need a function which returns itas a string - thanks! (see example below)

 function ConvertSecondsToString(Seconds: Integer): String
 begin
    Result :=  (formatted as such : 'Hour:Min:Secs')
  end;
0
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
LVL 11

Author Comment

by:shaneholmes
Comment Utility
Trekker72 - Thats what i was looking for - excellent - Thanks!

Shane
0
 
LVL 6

Expert Comment

by:DaFox
Comment Utility
Hm, that wasn't that difficult to adjust. Anyway, this would be my function:

procedure TForm1.Button1Click(Sender: TObject);

  function ConvertSecondsToTime(iSeconds: Word): String;
  var
    hr, min: Word;
  begin
    hr := trunc(iSeconds / 3600);
    iSeconds := iSeconds - (3600 * trunc(iSeconds / 3600));
    min := trunc(iSeconds / 60);
    iSeconds := iSeconds - (60 * trunc(iSeconds / 60));
    Result := Format('%.2d:%.2d:%.2d', [hr, min, iSeconds]);
  end;

begin
  ShowMessage(ConvertSecondsToTime(42300));
end;

Regards,
Markus
0
 
LVL 2

Expert Comment

by:gary_williams
Comment Utility
Wouldn't
  Seconds div 3600
be more elegant than
  Trunc(Seconds / 3600)
and
  Floor(Seconds / 3600)
?
0
 
LVL 6

Expert Comment

by:DaFox
Comment Utility
You're right, gary! Using div is much more elegant. Forgot about it :/
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

15 Experts available now in Live!

Get 1:1 Help Now