views:

169

answers:

2

Hi,

I am developing a timer in Delphi 2009. I am currently using the following to format my timer display:

Caption := Format('%.2d', [Hours]) + ':' + 
           Format('%.2d', [Minutes]) + ':' + 
           Format('%.2d', [Seconds]);

and this as expected displays the time as:

00:04:35

However, when I go into negative time it is understandably displaying it as:

00:-04:-35

I need the time to display as:

-00:04:35

Any Ideas?

+11  A: 

Hi, try this

Prefix:='';
if (Hours<0) or (Minutes<0) or (Seconds<0) then
Prefix:='-';

Caption := Prefix+Format('%.2d', [Abs(Hours)]) + ':' + 
           Format('%.2d', [Abs(Minutes)]) + ':' + 
           Format('%.2d', [Abs(Seconds)]);

Bye.

RRUZ
Thanks can't believe how simple this was...think I have been staring at it too long...
James
Why don't you call `Format('%.2d:%.2d:%.2d', [...])` with all three values in one go?
mghie
Ah never though of that mghie thanks!
James
A: 

Well, you're formatting each number separately, therefore its no surprise you get negative sign on all of them. Try this:

Caption := Format('%.2d', [Abs(Hours)]) + ':' + 
           Format('%.2d', [Abs(Minutes)]) + ':' + 
           Format('%.2d', [Abs(Seconds)]);

if (Hours < 0) or (Minutes < 0) or (Seconds < 0) then begin
  Caption := '-' + Caption;
end;
Daniil
Nice, voted up as helpful even though it doesn't even compile.
mghie
Sorry, I haven't done any Pascal since HS :)
Daniil
Syntax errors fixed.
mghie