Calculate the difference between two time values (Views: 711)
Problem/Question/Abstract: How to get the hours and minutes between two DateTime values? Answer: Solve 1: In order to avoid future questions about rounding, I would consider the solution below (untested!): { ... } var InTime, OutTime: TDateTime; InMinutes, OutMinutes, MinutesDiff: Int64; DiffHours, DiffMinutes: Integer; begin { First, make sure that InTime and OutTime are relative to the same offset from GMT. Then:} InMinutes := round(InTime * 24 * 60); OutMinutes := round(OutTime * 24 * 60); MinutesDiff := OutMinutes - InMinutes; Assert(MinutesDiff >= 0); DiffHours := MinutesDiff div 60; DiffMinutes := MinutesDiff mod 60; end; Solve 2: { ... } var InTime, OutTime, TimeDifference: TDateTime; DiffHours, DiffMinutes: Integer; begin TimeDifference := OutTime - InTime; {ShowMessage(DateTimeToStr(TimeDifference);} DiffHours := trunc(TimeDifference * 24); { DiffMinutes := trunc((TimeDifference * 24 * 60) - (Trunc(TimeDifference * 24) * 60))} DiffMinutes := trunc((TimeDifference * 24 * 60) - (DiffHours * 60)) end; |