Board index » delphi » Why won't this daysbetween code work ???

Why won't this daysbetween code work ???

Hello All,

Why does this code give me the value  '37198'

when I think it should read '4'.

------------

begin

Label1.Caption := IntToStr (DaysBetween (now,28/10/2001));

end;

------------

I know TDate functions are based on counting from 1899, but I can't
figure this one out. I have tried all sorts of date permutations, used
variable and constants to represent the values, but no joy.

PS It is so I can keep a day counter on the status bar telling me how
long since I have stopped smoking.

Liam Healy

 

Re:Why won't this daysbetween code work ???


On Sat, 3 Nov 2001 15:53:18 +0000, "L. Healy"

Quote
<L.He...@occpsy.demon.co.uk> wrote:
>Hello All,

>Why does this code give me the value  '37198'

>when I think it should read '4'.
>Label1.Caption := IntToStr (DaysBetween (now,28/10/2001));

Your 28th of October is just an expression saying divide 28 by 10,
then by 2001.  You need to convert it into a date using EncodeDate or
some other function.

Duncan Murdoch

Re:Why won't this daysbetween code work ???


On Sat, 3 Nov 2001 15:53:18 +0000, "L. Healy"

Quote
<L.He...@occpsy.demon.co.uk> wrote:
>Hello All,

>Why does this code give me the value  '37198'

>when I think it should read '4'.

>------------

>begin

>Label1.Caption := IntToStr (DaysBetween (now,28/10/2001));

>end;

>------------

>I know TDate functions are based on counting from 1899, but I can't
>figure this one out. I have tried all sorts of date permutations, used
>variable and constants to represent the values, but no joy.

Did you copy that directly from your code?  If so, what you're passing
to the second parameter is a floating point expression:

  28 / 10 / 2001 {i.e. 28 divided by 10 divided by 2001 }

I'm guessing DaysBetween() takes two TDateTimes?  Since TDateTime is
an "alias" for a double, this would likely compile, but probably not
return what you expect.

I suspect what you want is something more like:

  IntToStr(DaysBetween(now, StrToDate('28/10/2001')));

HTH

Stephen Posey
slpo...@concentric.net

Re:Why won't this daysbetween code work ???


JRS:  In article <r$gJ5CAuLB57E...@occpsy.demon.co.uk>, seen in
news:comp.lang.pascal.delphi.misc, L. Healy <L.He...@occpsy.demon.co.uk>
wrote at Sat, 3 Nov 2001 15:53:18 :-

Quote
>Hello All,

>Why does this code give me the value  '37198'

>when I think it should read '4'.
>Label1.Caption := IntToStr (DaysBetween (now,28/10/2001));

Because today is Day 37198 and the time is probably 0.5 +-0.25; and
28/10/2001 is a double-division with result about 0.0014.

You need to EncodeDate(2001, 10, 28); and to expect 6.

--
? John Stockton, Surrey, UK.  j...@merlyn.demon.co.uk   Turnpike v4.00   MIME. ?
 <URL: http://www.merlyn.demon.co.uk/> TP/BP/Delphi/&c., FAQqy topics & links;
 <URL: http://www.merlyn.demon.co.uk/clpb-faq.txt> Pedt Scragg: c.l.p.b. mFAQ;
 <URL: ftp://garbo.uwasa.fi/pc/link/tsfaqp.zip> Timo Salmi's Turbo Pascal FAQ.

Other Threads