Forum > LCL
[Solved] Comparing dates
(1/1)
Riccardo Giuliani:
Hi everybody
I'm getting troubled with a date comparison inside a ttimer object, OnTimer event:
--- Code: ---procedure TForm1.compareTimer(Sender: TObject);
var
i: integer;
aux: TDateTime;
begin
for i:=0 to ListOfNotifications.Items.Count-1 do
begin
aux := TMyNotify(ListOfNotifications.Items[i]).Get_MyDate();
if SameDateTime(Now, aux) then
notify();
end;
end;
--- 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?
Thanks
Guionardo Furlan:
Hello
You can use this DateTimeCompare function to test:
--- Code: ---function DateTimeIsSame(const AD1,AD2: TDateTime): Boolean;
const Interval100ms = 8640000;
begin
Result:=Trunc(AD1*Interval100ms)=Trunc(AD2*Interval100ms);
end;
--- 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)
Bye
Navigation
[0] Message Index