Board index » delphi » Format Seconds

Format Seconds


2005-04-28 05:13:11 AM
delphi189
Is there a quick way or component to take the time in-between two DateTime
arguments and format it like this:
00:00:00
Where 123 seconds would be 00:02:03
Thanks!
Doug
 
 

Re:Format Seconds

"Doug Olson" <XXXX@XXXXX.COM>skrev i en meddelelse
Quote
Is there a quick way or component to take the time in-between two DateTime
arguments and format it like this:
00:00:00
Where 123 seconds would be 00:02:03
var
Diff: TDateTime;
begin
Diff := Now;
Sleep(3000);
Diff := Now - Diff;
// Use the user's locale settings
Caption := FormatDateTime('hh:nn:ss', diff);
// always use : regardless of user's locale settings
Caption := FormatDateTime('hh":"nn":"ss', diff);
--
Finn Tolderlund
 

Re:Format Seconds

Doug Olson writes:
Quote
Is there a quick way or component to take the time in-between two
DateTime arguments and format it like this:

00:00:00

Where 123 seconds would be 00:02:03
uses DateUtils;
Caption := FormatDateTime('hh:nn:ss', IncSecond(0.0, 123));
--
-Mike (TeamB)
 

Re:Format Seconds

"Mike Williams (TeamB)" <XXXX@XXXXX.COM>writes
Quote
Doug Olson writes:

Caption := FormatDateTime('hh:nn:ss', IncSecond(0.0, 123));

in D6, mm or nn seem to be equivalent ???
IOW FormatDateTime('yy:mm:dd:hh:mm:ss', IncSecond(DATE, 123)) produces
correct output.
Robert
 

Re:Format Seconds

Quote
// Use the user's locale settings
Caption := FormatDateTime('hh:nn:ss', diff);
// always use : regardless of user's locale settings
Caption := FormatDateTime('hh":"nn":"ss', diff);
only works where diff < 1 (24 hours). problematic if it is>= 1. I haven't
found an easy solution for values>= 1.
Ed Dressel
 

Re:Format Seconds

"Ed Dressel" wrote
Quote
only works where diff < 1 (24 hours).
Try this:
function TimeDif(TimeDif: double): string;
var tot: Int64; sec, min, hrs: integer;
begin
tot := round(TimeDif*24*60*60);
If tot < 0
then begin Result := '-'; tot := -tot end
else Result := '';
sec := tot mod 60; tot := tot div 60;
min := tot mod 60; hrs := tot div 60;
Result := Result + Format('%D:%.2D:%.2D',[hrs,min,sec]);
end;
{UNTESTED}
 

Re:Format Seconds

Quote
Is there a quick way or component to take the time in-between two DateTime
arguments and format it like this:

00:00:00

Where 123 seconds would be 00:02:03
There is a Delphi language newsgroup as well! ;)
Lauchlan M
 

Re:Format Seconds

Quote
IOW FormatDateTime('yy:mm:dd:hh:mm:ss', IncSecond(DATE, 123)) produces
correct output.
I really hope not! Except perhaps in February. mm is month, and nn is
minutes.
/Matthew Jones/
 

Re:Format Seconds

Thanks for responses! After I posted this message, it did occur to me that
this may not be the appropriate group, (sorry) although the responses were
great!
Doug
"Lauchlan M" <XXXX@XXXXX.COM>writes
Quote
>Is there a quick way or component to take the time in-between two
>DateTime
>arguments and format it like this:
>
>00:00:00
>
>Where 123 seconds would be 00:02:03

There is a Delphi language newsgroup as well! ;)

Lauchlan M


 

Re:Format Seconds

"Matthew Jones" <XXXX@XXXXX.COM>writes
Quote
>IOW FormatDateTime('yy:mm:dd:hh:mm:ss', IncSecond(DATE, 123)) produces
>correct output.

I really hope not!
It does. Try it.
Robert
 

Re:Format Seconds

Robert writes:
Quote
It does. Try it.
Yes, you can use either 'M' or 'N' for minutes but only because of a
small hack in the formatting routine:
if (Token = 'M') and (LastToken = 'H') then Token := 'N';
In other words, it automatically converts 'M' to 'N' if used right
after an 'H'. I prefer to use 'N' out of habit.
--
-Mike (TeamB)
 

Re:Format Seconds

<snip>
I know--I inferred from the original post that "Is there a quick way" meant
~one line. I have something similar to what you wrote in my toolset.
--
Ed Dressel
 

Re:Format Seconds

Too many people making the mistake I guess.
I think I would not use that since one day I will want to make it YYNN or
something, and the HH would be missing and then I would be wondering why it
was broken.
/Matthew Jones/