Delphi Pages Forums  

Go Back   Delphi Pages Forums > Delphi Forum > General

Lost Password?

Reply
 
Thread Tools Display Modes
  #1  
Old 09-19-2013, 01:27 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default Time results in decimal

Hi All and thanks for the help

I have this code which will calculate the time between two dates time

The format result is in hours and minutes, however, I need to change it to decimals

Current Result in the below code is 05:30 (Five Hours and 30 minutes)
Result I need is 5.5 etc.

Any help will be strongly appreciated.

Thanks

Code:
procedure TForm2.Button1Click(Sender: TObject);
var
Hour, Min, Sec, MSec: Word;
dt1 : TdateTime;
dt2 : TdateTime;
TimeAmount : TTime;
begin
dt2 := StrToDateTime('09/02/2013 07:30 PM');
dt1 := StrToDateTime('09/03/2013 01:00 AM');
DecodeTime(dt2-dt1, Hour, Min, Sec, MSec);
TimeAmount := dt2-dt1;
end;
Reply With Quote
  #2  
Old 09-19-2013, 02:11 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default

Thanks for the help, however I was able to get it going my self.

Here is what I did so that if others come across it will be a help to them

Code:
//Declare  a Function

function MyConvert(d:TDateTime): Extended;
begin
Result:=Frac(d)*24;
end;

Code:
//the previous posted procedure was actually incorrect, so it was fixed and //then the function was called
procedure TForm2.Button1Click(Sender: TObject);
var
Hour, Min, Sec, MSec: Word;
dt1 : TdateTime;
dt2 : TdateTime;
TimeAmount : TTime;
begin
dt2 := StrToDateTime('09/02/2013 07:30 PM');
dt1 := StrToDateTime('09/03/2013 01:00 AM');
DecodeTime(dt1-dt2, Hour, Min, Sec, MSec);
TimeAmount := dt1-dt2;

//MyConvert(TimeAmount);

ShowMessage(formatCurr('#.##',MyConvert(TimeAmount)));
end;
Reply With Quote
  #3  
Old 09-19-2013, 02:18 PM
Norrit Norrit is offline
Moderator
 
Join Date: Aug 2001
Location: Landgraaf
Posts: 7,335
Default

Not sure if I like this code, but it should do the trick:
Code:
procedure TForm1.Button1Click(Sender: TObject);
const
  factor: real = 10/6;
var
  Hour, Min, Sec, MSec: Word;
  dt1 : TdateTime;
  dt2 : TdateTime;
  TimeAmount : TTime;
  fmt: TFormatSettings;
  myTimeFloat: Real;
begin
  fmt.DateSeparator := '/';
  fmt.TimeSeparator := ':';
  fmt.LongDateFormat := 'dd-mmm-yyyy';
  fmt.ShortDateFormat := fmt.LongDateFormat;
  fmt.LongTimeFormat := 'hh:nn';
  fmt.ShortTimeFormat := fmt.LongTimeFormat;

  dt2 := StrToDateTime('09/02/2013 07:30 PM', fmt);
  dt1 := StrToDateTime('09/03/2013 01:00 AM', fmt);
  DecodeTime(dt2-dt1, Hour, Min, Sec, MSec);
  TimeAmount := dt2-dt1;

  DecodeTime(TimeAmount, Hour, Min, Sec, MSec);

  myTimeFloat := StrToFloat(IntToStr(Hour) + DecimalSeparator + IntToStr(Trunc(Min*factor)));
  ShowMessage(FloatToStr(myTimeFloat));
end;
Trick is done in the myTimeFloat := line...
Reply With Quote
  #4  
Old 09-19-2013, 02:34 PM
delphimpd delphimpd is offline
Senior Member
 
Join Date: Oct 2006
Posts: 701
Default

Hi Norrit,

Thanks for the code.

Regards,
Reply With Quote
  #5  
Old 09-19-2013, 05:36 PM
major major is offline
Senior Member
 
Join Date: Jun 2011
Posts: 150
Default

Code:
uses
   DateUtils;

function Convert(StartDT, EndDT: TDateTime): Extended;
var
   H, M, S, MS: Word;
begin
   DecodeTime(StartDT - EndDT, H, M, S, MS);
   Result := (DaysBetween(StartDT-EndDT)*24)+H+(S/60)+(MS/10000);
end;
Reply With Quote
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is On

Forum Jump


All times are GMT. The time now is 10:39 AM.


Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2019, vBulletin Solutions, Inc.