Forum > LCL

[Solved] Comparing dates


Riccardo Giuliani:
Hi everybody

I'm getting troubled with a date comparison inside a ttimer object, OnTimer event:

--- Code: ---procedure TForm1.compareTimer(Sender: TObject);
 i: integer;
 aux: TDateTime;
 for i:=0 to ListOfNotifications.Items.Count-1 do
   aux := TMyNotify(ListOfNotifications.Items[i]).Get_MyDate();
   if SameDateTime(Now, aux) then
--- End code ---

I pick date and time from a ZVdatetime object (Get_MyDate() function) and problem is that SameDateTime(Now, aux) comparison fails almost always, except once every n-th try.
After multiple checkings i thought it can be a matter of milliseconds: ZVdatetime picker include a displaying format with them, even if apparently they don't seem to be important.
I set timer interval to 50 ms and it's always enabled.

Have you any suggestion?

Guionardo Furlan:

You can use this DateTimeCompare function to test:

--- Code: ---function DateTimeIsSame(const AD1,AD2: TDateTime): Boolean;
const Interval100ms = 8640000;
--- End code ---

The Interval100ms const is: 24 hours * 60 minutes * 60 seconds * 100 centiseconds
This value give an 1/100 second precision to the function.

See if this works.

Riccardo Giuliani:
First of all thank you.
I've done according to your code, but it doesn't work.
Good news is you drove me to verify if even Now function returns milliseconds: it's so!
Well, solving just the side of zvdatetimepicker was the half part. Now i locked the condition this way:

--- Code: ---...
aux1 := Now;
aux2 := TMyNotify(ListOfNotifications.Items[i]).Get_MyDate();
if (aux1 >= IncMilliSecond(aux2, 300)) and (aux1 <= IncMilliSecond(aux2, 600)) then

--- End code ---

I had to abandon trunc function because it truncs every thing below the day unit, so that when i checked - with showmessage - truncated both aux1 and aux2 i got [00:00:00] (ms not shown) with condition always verified.  8)



[0] Message Index

Go to full version