Solved

Delphi, how to Format DateTime values bigger than 24h in 'hh:nn:ss' format

Posted on 2014-08-01
5
1,388 Views
Last Modified: 2014-08-06
Hello,
I need to format in 'hh:nn:ss' values bigger than 24h using Delphi.

FormatDateTime('hh:nn:ss', FloatToDateTime(2));

Open in new window


I'm expecting '48:00:00' but resulting value is '00:00:00' because days part isn't shown.
How can I get '48:00:00'?
Thanks for your willingness.
0
Comment
Question by:ExDev001
5 Comments
 
LVL 33

Expert Comment

by:ste5an
ID: 40233947
There is no such thing like a  DateTime value bigger than 24.  Use a TTimeSpan instead. E.g.
program Project8;
{$APPTYPE CONSOLE}
{$R *.res}

uses
  System.SysUtils, System.TimeSpan, System.DateUtils;

var
  ts: TTimeSpan;

begin
  FormatSettings.DateSeparator := '/';
  ts := TTimeSpan.FromSeconds(SecondsBetween(StrToDateTime('01/10/2014'), StrToDateTime('01/07/2014 12:00:00 a.m.')));
  WriteLn(Format('%dd %dh %dm', [ts.Days, ts.Hours, ts.Minutes]));
  WriteLn(#13#10 + 'Press any key to quit.');
  ReadLn;

end.

Open in new window

0
 
LVL 37

Accepted Solution

by:
Geert Gruwez earned 500 total points
ID: 40238330
format the hours yourself and then the minutes and seconds as before

function fdt(x: Double): string;
begin
  if x >= 1 then 
    Result := Format('%d', [Trunc(x*24)]) + FormatDateTime(':nn:ss', FloatToDateTime(x))
  else 
    Result := FormatDateTime('hh:nn:ss', FloatToDateTime(x));
end;

Open in new window

0
 
LVL 19

Expert Comment

by:Thommy
ID: 40238407
Delphi standard functions FormatDateTime and TimeToStr do not handle more than 24 hours.

You should definitely write your own TimeToStr function;
function MyTimeToStr(MyStartDT,MyEndDT:TDateTime):string...
var
  d,h,i: integer;
  s: string;
  TStart,TEnd: TTime;
begin
  TStart:= MyStartDT;
  TEnd  := MyEndDT;
  s     := TimeToStr(TStart-TEnd);

  i     := StrToInt(Copy(s,1,2));
  d     := Int(MyEndDT)-Int(MyStartDT);
  h     := d*24+i;
  result:=IntToStr(h)+copy(s,3,5);
end;

Open in new window

0
 

Author Closing Comment

by:ExDev001
ID: 40243318
Clean solution! It works fine.
Thanks alot for your willingness.
0
 
LVL 33

Expert Comment

by:ste5an
ID: 40243381
What's wrong with using an accurate data type?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this tutorial I will show you how to use the Windows Speech API in Delphi. I will only cover basic functions such as text to speech and controlling the speed of the speech. SAPI Installation First you need to install the SAPI type library, th…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

867 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now