Solved

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

Posted on 2014-08-01
5
1,727 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 34

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 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 34

Expert Comment

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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month10 days, 17 hours left to enroll

628 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