Solved

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

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

Expert Comment

by:Stefan Hoffmann
Comment Utility
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 36

Accepted Solution

by:
Geert Gruwez earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
Clean solution! It works fine.
Thanks alot for your willingness.
0
 
LVL 32

Expert Comment

by:Stefan Hoffmann
Comment Utility
What's wrong with using an accurate data type?
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

772 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

10 Experts available now in Live!

Get 1:1 Help Now