Solved

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

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Working this component WebCopy with proxy, help 3 100
CheckListBox usage 3 81
Firemonkey BASS_Init into a thread 17 54
Broadcast a message using ICS 2 23
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

749 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