Solved

# Elapsed time in text

Posted on 2006-05-15
399 Views
Hi,

Anyone has a function in that can convert elapsed time to text ?

For example:

StartTime := Now;
// Do something in here for quite a long time
EndTime := Now;

ElapsedTime := <EndTime - StartTime>

Then convert it to text, something like "3 days, 20 hours, 34 minutes, 5
seconds".

Thanks for any help.
0

LVL 5

Expert Comment

procedure TForm1.Button1Click(Sender: TObject);
var
starttime, endtime, elapsedtime : tdatetime;
a1, a2, a3, a4 : string;
a11 : integer;
begin
StartTime := Now;
sleep(1000);
EndTime := Now;

ElapsedTime := EndTime - StartTime;

a1 := formatdatetime('dd',elapsedtime);
a11 := strtoint(a1) - 30;
//im doing this because it showed 30 days when timing 1sec so i - 30 to get 0
//mabey if you do a whole day time test on this it might show negative number then you can just work around that
a2 := formatdatetime('hh',elapsedtime)+' Hours,  ';
a3 := formatdatetime('nn',elapsedtime)+' Minutes,  ';
a4 := formatdatetime('ss',elapsedtime)+' Seconds';

label1.Caption := inttostr(a11)+' Days,  '+a2+a3+a4;

end;

Peace Scay7
0

LVL 5

Expert Comment

i would also suggest...

http://www.devexpress.com/Products/VCL/ExForumLibrary/

It has a very nice TIMER that can count down and upwards, with some other nice utils which is free

Peace Scay7
0

LVL 3

Expert Comment

You may wish to make it more intelligent by not displaying portions unnecessary.

also. DateUtils.pas d7 upwards has many simple routines to make it easy

if DaysBetween(Finish,Start) >= 1 then
result := result + format('%d days',[trunc(DaysBetween(Finish,Start)]);

etc using hoursbetween, miniutes between down to milliseconds between.

0

LVL 17

Accepted Solution

Here's a routine I use for 2 purposes
the first, is to show the time as you describe above,
the seconds, is to show only a low detail of the time, for use in progress bars
e.g.
ShowAllDetail: True                         ShowAllDetail: False

1 second                                              1 second
14 seconds                                           14 seconds
15 seconds                                           15 seconds
59 seconds                                            59 seconds
1 minute                                                1 minute
1 minute, 1 second                                 1 minute
1 minute, 14 seconds                              1 minute
1 minute, 15 seconds                              1 minute, 15 seconds
59 minutes, 59 seconds                           59 minutes, 45 seconds
1 hour                                                   1 hour
1 hour, 1 second                                      1 hour
1 hour, 14 seconds                                  1 hour
1 hour, 1 minute                                      1 hour
1 hour, 1 minute, 15 seconds                     1 hour
1 hour, 1 minute, 1 second                        1 hour
1 hour, 14 minutes, 1 second                     1 hour
1 hour, 15 minutes, 1 second                     1 hour, 15 minutes
1 day                                                       1 day
1 day, 1 hour                                            1 day, 1 hour
1 day, 23 hours                                         1 days, 23 hours
1 day1, 23 hours, 59 minutes, 59 seconds    1 days, 23 hours

function DurationAsString(Duration_: TDateTime; const ShowAllDetail: boolean): string;
var
h, n, s, ss: word;
days_string: string;
hours_string, minutes_string, seconds_string: string;
hasabovedata: boolean;
begin
hasabovedata := False;
if Trunc(Duration_) = 0 then
days_string := ''
else if Trunc(Duration_) = 1 then
days_string := IntToStr(Trunc(Duration_)) + ' day'
else
days_string := IntToStr(Trunc(Duration_)) + ' days';

hasabovedata := (days_string <> '');

DecodeTime(Duration_, h, n, s, ss);
if h = 1 then
hours_string := '1 hour'
else if (h <> 0) then
hours_string := IntToStr(h) + ' hours';

if n = 1 then
minutes_string := '1 minute'
else if (n <> 0) then
minutes_string := IntToStr(n) + ' minutes';

if s = 1 then
seconds_string := '1 second'
else if (s <> 0) then
seconds_string := IntToStr(s) + ' seconds';
result := '';
if showalldetail then
begin
if days_string <> '' then
result := days_string +', ';
if hours_string <> '' then
result := result + hours_string + ', ';
if minutes_string <> '' then
result := result + minutes_string + ', ';
if ( ShowAllDetail or ((result = '') and (seconds_string <> '')) ) then
result := result + seconds_string;
end
else
begin // Change the settings below to only show the level of detail you wish
if days_string <> '' then
begin
result := days_string +', ';
if (h > 0) then result := result + hours_string;
end
else if hours_string <> '' then
begin
result := result + hours_string + ', ';
if ( n div 15 > 0 ) then
result := result + IntToStr((n div 15) * 15) + ' minutes';
end
else if minutes_string <> '' then
begin
result := result + minutes_string + ', ';
if ( s div 15 > 0)  then
result := result + IntToStr((s div 15) * 15) + ' seconds';
end
else if ( ShowAllDetail or ((result = '') and (seconds_string <> '')) ) then
result := result + seconds_string;
end;

while ( (length(result) > 0) and
(
(result[length(result)] = ',') or
(result[length(result)] = ' ')
)
) do
delete(result, length(result), 1);
end;

0

LVL 1

Author Comment

TheRealLoki, interesting function. What is "Duration_" in the parameter ? How do you calculate that ?

Can I just use simple substraction from 2 TDatetime variables ?

Duration_ := StartTime - EndTime;

0

LVL 1

Author Comment

Ok, I got it.

But what is "hasabovedata" used for ?
0

LVL 1

Author Comment

Thanks, I figured it out :)

0

LVL 17

Expert Comment

yes
var
starttime, endtime: tdatetime;
...
label1.caption := DurationAsString(endtime-starttime, True);

"hasabovedata" is not needed, it's leftover code from a previous way i had it written
Sorry, i haven't updated this function in many moons
0

## Featured Post

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
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…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…