Recent

Author Topic: [Solved] Comparing dates  (Read 3900 times)

Riccardo Giuliani

  • New Member
  • *
  • Posts: 40
[Solved] Comparing dates
« on: July 21, 2010, 02:53:59 pm »
Hi everybody

I'm getting troubled with a date comparison inside a ttimer object, OnTimer event:
Code: [Select]
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;  

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
« Last Edit: July 21, 2010, 05:13:42 pm by Riccardo Giuliani »

Guionardo Furlan

  • New Member
  • *
  • Posts: 18
    • Guionardo Furlan
Re: Comparing dates
« Reply #1 on: July 21, 2010, 03:43:45 pm »
Hello

You can use this DateTimeCompare function to test:

Code: [Select]
function DateTimeIsSame(const AD1,AD2: TDateTime): Boolean;
const Interval100ms = 8640000;
begin
  Result:=Trunc(AD1*Interval100ms)=Trunc(AD2*Interval100ms);
end;

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.

« Last Edit: July 21, 2010, 03:49:56 pm by Guionardo Furlan »

Riccardo Giuliani

  • New Member
  • *
  • Posts: 40
Re: Comparing dates
« Reply #2 on: July 21, 2010, 05:13:15 pm »
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: [Select]
...
aux1 := Now;
aux2 := TMyNotify(ListOfNotifications.Items[i]).Get_MyDate();
...
if (aux1 >= IncMilliSecond(aux2, 300)) and (aux1 <= IncMilliSecond(aux2, 600)) then

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

 

TinyPortal © 2005-2018