Solved

elapsed seconds as hours:minutes:seconds

Posted on 2003-11-11
7
758 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 6

Expert Comment

by:DaFox
ID: 9726640
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
ID: 9726706
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
ID: 9726711
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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 11

Author Comment

by:shaneholmes
ID: 9726749
Trekker72 - Thats what i was looking for - excellent - Thanks!

Shane
0
 
LVL 6

Expert Comment

by:DaFox
ID: 9726805
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
ID: 9733248
Wouldn't
  Seconds div 3600
be more elegant than
  Trunc(Seconds / 3600)
and
  Floor(Seconds / 3600)
?
0
 
LVL 6

Expert Comment

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

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

739 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