Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 406
  • Last Modified:

Elapsed time in text

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
db2trade
Asked:
db2trade
  • 3
  • 2
  • 2
  • +1
1 Solution
 
Scay7Commented:
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
 
Scay7Commented:
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
 
KyleyHarrisCommented:
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
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
TheRealLokiSenior DeveloperCommented:
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
 
db2tradeAuthor Commented:
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
 
db2tradeAuthor Commented:
Ok, I got it.

But what is "hasabovedata" used for ?
0
 
db2tradeAuthor Commented:
Thanks, I figured it out :)

0
 
TheRealLokiSenior DeveloperCommented:
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

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 3
  • 2
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now