Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

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

Posted on 2014-08-01
5
Medium Priority
?
1,910 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 35

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 38

Accepted Solution

by:
Geert Gruwez earned 2000 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 35

Expert Comment

by:ste5an
ID: 40243381
What's wrong with using an accurate data type?
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.

Question has a verified solution.

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

Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

730 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