Time Difference (Minutes Between) lazarus / 61-Beginners ---------------------------------------------------------------------- pipingae · 2014-11-20 08:24 Hai ... I'm trying to use MinutesBetween in lazarus, but kind of confused since the result (according to me) is inaccurate. Each time the minutes difference is 240 minutes the result is 239 minutes. Whats wrong ? any one help ? this is the simple code that i write procedure TForm1.FormCreate(Sender: TObject); var timeAwal,timeAkhir:TDateTime; begin   timeAwal:=StrToDateTime('2014-11-01 06:00:00');   timeAkhir:=StrToDateTime('2014-11-01 10:02:00');   showMessage(IntToStr(MinutesBetween(timeAkhir,timeAwal))); end;        the correct minutes should have ben 240 minutes, i include the screen shot attachment ---------------------------------------------------------------------- JZS · 2014-11-20 09:08 Hi pipingae, I see it's your first post here so, welcome. About your code: I tested your code and it gives the expected result 240. BTW you mentioned "timeAkhir" to be 10:02:00 in your post, but I corrected it to be 10:00:00 to match the screenshot and it works OK. Even when it was 10:02:00 it gives 242 which is correct also. Which version of Lazarus you are using? ---------------------------------------------------------------------- wp · 2014-11-20 09:26 This could be due to rounding errors. As far as I know MinutesBetween uses the trunc function which is off by one if the value to be rounded turns out like 239.9999999998. I'd use an own function: Code: [Select]function MinutesBetween(ADateTime1, ADateTime2:TDateTime): Integer; const   MINUTES_PER_DAY = 24*60;   EPS = 1E-6;  // adjust value var   minutes: Double; begin   minutes := (ATime2 - ATime1) / MINUTES_PER_DAY;   if frac(minutes) > (1.0-EPS) then  // detect rounding error; EPS may require some adjustment     Result := round(minutes)   else     Result := trunc(minutes);  // consider only full minutes end ; ---------------------------------------------------------------------- pipingae · 2014-11-20 10:05 I'm very very sorry, the correct timeAkhir was 10:00:00. I'm using Lazarus 1.2.4 Win 32 version on Windows 8.1 64 Bit Still i'm getting 239 minutes result, other time difference is good. Any help ---------------------------------------------------------------------- pipingae · 2014-11-20 10:09 i've try changing the timeAkhir, i include the screenshot, only when the time begin at 07:00:00 the result is correct, other than that it give me 239 minutes .... ---------------------------------------------------------------------- JZS · 2014-11-20 10:21 I tried on Laz1.2.4, and as was expected, the result is correct same as on Laz1.2.6. What do you get if you try MinuteSpan()? Try it please: Code: [Select]showMessage('Min. Span: '+ floattostr(MinuteSpan(timeAkhir, timeAwal))); What about setting the time part only: Code: [Select]procedure TForm1.FormCreate(Sender: TObject); var   timeAwal,   timeAkhir      :TTime; begin   timeAwal:= StrToTime('06:00:00');   timeAkhir:= StrToTime('10:00:00');   showMessage('MinutesBetween '+ IntToStr(MinutesBetween(timeAkhir, timeAwal))   +#13#10+ 'TimeSpan '+ floattostr(MinuteSpan(timeAkhir, timeAwal))); end; ---------------------------------------------------------------------- howardpc · 2014-11-20 11:02 All floating-point calculations are inherently approximate. Identical TDateTime code run on different computers always has the possibility that its results might differ by some tiny increment. Rounding tries to eliminate such differences, but they depend on factors that are not always known or controllable (floating point computations done in FPU or by emulation, for instance). Using strings for datetime values and integer arithmetic for operations is a better way to guarantee consistent precision within a stated increment. TDateTime as a double has approximation built in to it. A further point is what "MinutesBetween" is understood to mean. The docs for this function say     "Calculate the number of whole minutes between two DateTime values." Some might argue that the number of whole minutes between   01/11/2014 10:00:00 and   01/11/2014 10:01:00 is 0 and not 1 (some mathematicians are like that). ---------------------------------------------------------------------- pipingae · 2014-11-20 11:05 Hi I've the minutespan, and i think the result is the same 239 if using the TDateTime variable type, but if we only use TTime variable, the result is accurate (240 minutes) i've include the screenshot