Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

datetime comparison

Status
Not open for further replies.

bobbygm

Programmer
Mar 30, 2005
19
IT
HI!

I NEED HELP!!

I have problems while comparing two dates in
tdatetime format with Delphi 7.
I compare the two variables either by > or
by the comparedatetime function, and the
result of the comparison seems to be unpredictable.
It doesn't give correct results, and I don't know
why.
Any good advice??
Thank u

Emanuele
 
Can you post some code which demonstrates the problem? Please show any error messages or evidence of why you are not getting the results you expect.

Andrew
Hampshire, UK
 
this is the code statement:

if comparedate(data_tm,data_old)>=0 then ...

(data_tm and data_old are both variables of Tdatetime type)

and this is part of a log file in which I manually
added an ending "---" to indicate a wrong result and "-"
to indicate equal values (this is a lesser problem):

41859A3W --- 29-nov-04 11:52:22 < 09-feb-89 18:16:32---
41860A3W --- 17-mar-89 14:44:06 < 13-giu-94 15:31:28
41958A3W --- 17-mar-89 14:45:40 < 13-giu-94 15:33:20
41988A3W --- 06-lug-99 11:45:22 < 14-apr-89 12:50:02---
41989A3W --- 09-feb-04 8:49:50 < 19-apr-89 9:30:50---
41990A1W --- 27-giu-00 8:31:04 < 03-mar-89 17:05:42---
41991A3W --- 04-mag-89 11:13:48 < 04-mag-89 11:13:48-
41992A3W --- 27-apr-89 9:40:48 < 27-apr-89 9:40:48-
41993A3W --- 23-feb-89 9:52:56 < 23-feb-89 9:52:56-
41994A3W --- 31-gen-89 11:17:24 < 31-gen-89 11:17:24-
42015A3W --- 25-nov-88 11:33:00 < 19-mar-97 12:36:40
42017A3W --- 25-nov-88 18:56:12 < 06-mag-97 9:45:56
42019A3W --- 22-feb-89 16:05:36 < 19-mar-97 12:38:30
42026A3M --- 19-apr-89 9:41:58 < 19-apr-89 9:41:58-
42030A4M --- 07-dic-88 17:23:50 < 07-dic-88 17:23:50-
42056A4M --- 25-mag-89 18:16:06 < 22-apr-94 18:12:36
42015A3W --- 10-apr-96 17:05:18 < 19-mar-97 12:36:40
42017A3W --- 10-apr-96 18:29:06 < 06-mag-97 9:45:56
42019A3W --- 08-giu-94 16:57:18 < 19-mar-97 12:38:30
42026A3M --- 09-feb-04 8:54:08 < 19-apr-89 9:41:58---
42027A4M --- 12-giu-04 11:43:22 < 07-dic-88 17:17:24---
42028A1W --- 01-mar-90 14:30:56 < 01-mar-90 14:30:56-
42030A4M --- 02-mag-89 15:04:52 < 07-dic-88 17:23:50---
42044A3I --- 27-apr-89 9:57:02 < 09-gen-89 9:52:24---

Thank u for your attention!

Emanuele
 
P.S. the log file is obtained by re-converting to
string the tdatetime values by the datetimetostr
function.

I noticed that the wrong results are in correspondance
with a second year previous to 1990. Can it be of some
importance?

Emanuele
 
Which version of Delphi are you using? I don't seem to have CompareDate function and i'm using D5Ent.

Or is it in a 3rd party library?
 
I'm using D7 ent.
You should include "dateutils" in the "uses" section.
Anyhow, "comparedate(a,b)>=0" is much the same as
"a>=b", when a and b are tdatetime.
If I use "a>=b", the program generates the same log file.
 
No dateutils in D5 ent. I can try to replicate the error. I'll post it here if i have any success.
 
You haven't supplied the actual code that produces the error.

For example, I produced a test program, using Delphi 7, that contained two TMonthCalender objects called A and B, a TButton and a TLabel and wrote this code for the OnClick handler of the TButton:
Code:
Uses DateUtils;

procedure TForm1.Button1Click(Sender: TObject);
begin
  case CompareDate ( A.Date, B.Date ) of
  -1: Label1.Caption := 'A is earlier than B';
   0: Label1.Caption := 'A is the sames as B';
  +1: Label1.Caption := 'A is later than B';
  end;
end;
CompareDate works perfectly okay in these circumstances with the test dates I used.

Please create a small test program that demonstrates the problem and copy and paste the code so that we can examine it. I am 99.99% sure that the error will be in your coding.

Andrew
Hampshire, UK
 
Now after towerbase's example it seems that you're only checking for positive result of CompareDate function.

if comparedate(data_tm,data_old)>=0 then ...

As towerbase's example shows you should infact use a 'case' statement instead of 'if'.
 
Ok, now the CODE is:

case comparedate(data_tm, data_old) of
1 : BLOGGER1.Logga(num_dis+' --- '+data_str+' > '+data_old_str);
0 : BLOGGER1.Logga(num_dis+' --- '+data_str+' = '+data_old_str);
-1 : BLOGGER1.Logga(num_dis+' --- '+data_str+' < '+data_old_str);
end;

with
data_tm, data_old : tdatetime
data_str:= datetimetostr(data_tm)
data_old_str:= datetimetostr(data_old)

BLOGGER1 is a simple component with a procedure "logga" that opens a log file and
writes text lines in it.

and the log file still shows the same results.

Is it possible that the problem arises from D7-english being installed in an Italian Win2000 environment?
I checked the proper OS date-time format settings, and they should be ok.
 
You still haven't shown us your actual code. For example, whereabouts is data_str and data_old_str being assigned?

Anyway, can you amend your case statement so that the actual numeric values of data_tm and data_old are logged then you should be able to see (and log) the actual values being compared.

I suggest you use the Format function to make life easier.

Something like:
Code:
const
  GT = '%s --- %s > %s. %f %f';
  EQ = '%s --- %s = %s. %f %f';
  LT = '%s --- %s < %s. %f %f';
...
case comparedate(data_tm, data_old) of
 1: BLOGGER1.Logga(Format(GT,[num_dis,data_str,data_old_str,data_tm,data_old));
 0: BLOGGER1.Logga(Format(EQ,[num_dis,data_str,data_old_str,data_tm,data_old));
-1: BLOGGER1.Logga(Format(LT,[num_dis,data_str,data_old_str,data_tm,data_old));
end;

Andrew
Hampshire, UK
 
Thank u very much for your advice.
Now the log file shows, for instance:

41859A3W --- 29-nov-04 11:52:22 < 09-feb-89 18:16:32. 38320,49 69073,76

something is obviously wrong.
the assignment to data_old is made by the following
dbExpress sqlquery and code:

SELECT num_dis, dir_dis, data_dis
FROM disegni
WHERE num_dis= ...
... SQLquery1.open ...
data_old:= fields.Fields[2].Asdatetime;

the field "data_dis" is of type "date" on an Oracle
database.

Where's the problem??
Thanks again



 

69073,76 is interpreted as 09-02-2089 18:14:24

38320,49 is interpreted as 29-11-2004 11:45:36

The problem appears to be a difference in the way Delphi and and your DBMS interpret 2-digit year values.

Since your DB is returning a date number of 69073 (year = 2089), I would say the real problem is where the data are being captured and stored in the DB originally.

 
OK.

Now that I know exactly where the problem is,
I can solve it (I hope).

Thanks for your attention and precision!

Emanuele
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top