Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chris Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How to get hours between two Tdatetime 1

Status
Not open for further replies.

michaenh

Programmer
Aug 7, 2002
205
NO
Hi.

Are there any easy functions to get hours between two dates?
An example I have been away from work on 07.07.2003 10:00 to 08.07.2003 18:00. I want to get the hours that I have been away... :)

Many thanks.
cheers,
mha
 
Use HourSpan or HoursBetween. Both take as input 2 TDateTime variables. HourSpan returns a result which includes fractional hours, whilst HoursBetween doesn't.

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
Here's an example:
Code:
var
  FirstDate, SecondDate: TDateTime;
begin
  FirstDate := EncodeDateTime(2003, 7, 7, 10, 0, 0, 0);
  SecondDate := EncodeDateTime(2003, 7, 8, 18, 0, 0, 0);
  ShowMessage(FloatToStr(HourSpan(FirstDate, SecondDate)));
end;

I am unsure as to whether you are British or American so check that I have entered the date correctly in the EncodeDateTime functions.

If you wanted whole hours only then use this line:
Code:
  ShowMessage(IntToStr(HoursBetween(FirstDate, SecondDate)));

Hope this helps!

Clive [infinity]
Ex nihilo, nihil fit (Out of nothing, nothing comes)
 
thanks for your quick answer!
Got it.. :)
Really happy... I am a really happy boy.. :)

thanks thanks.. cheers..
mha
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top