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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Datatypes

Status
Not open for further replies.

nicsin

Programmer
Jul 31, 2003
743
GB
Hello,

I have a function which determines if a date is in a "leap year". I need to pass the date as
Code:
TDateTime
datatype but I recreive it as
Code:
word
. The complier doesn't complain but I don't get the required result. If I hardcode a date in a
Code:
TDateTime
variable it seems to work fine. Has anyone encountered anything like this?

example:
This works (returns true)
Code:
var date: TDateTime;
date:=strtodatetime('1/1/2004');
isLeapYear(date);
This compiles but returns false.
Code:
var date: Word;
date:=2004;
isLeapYear(date);

Thnx a lot.
 
Your problem may stem from the fact that the value of 2004 in your second code sample is very different than the value of strtodatetime('1/1/2004'). If you need to pass a date to the function, you could do something like this:
Code:
function IsLeapYear(d: TDateTime): Boolean;
var y, m, d: Word;
begin
  DecodeDate(d, y, m, d);
  //do your processing on the y variable which will contain the year;
end;

-D
 
You can use the EncodeDate function as well:
[tt]function EncodeDate(Year, Month, Day: Word): TDateTime;[/tt]

The other way is as per the example in the help:
[tt]IsLeapYear(YearOf(MyDate));[/tt]
This will get around the problem of the correct typing consistently.

Importantly, these functions overcome the problem with different date formats around the world (or if users haven't set the format to local conventions!

Cheers


Chris ;-)
 
Only out of curiosity: how you managed to pass a TDateTime in a word parameter? What compiler version are you using?

buho (A).

 
That is a mystery to me as well buho! This code is written in delphi 2.0 three years ago. Now there is a bug with the 29/2 and I am investigating it. I haven't touched delphi before so this is a problem as well.

I will try your suggestions and come back. Thanx all!
 
Hello again,

I used the dubugger and navigated the coede line by line and apparently when 2004 was passed to the function as a word type, it produced the wrong results. I assigned the values of the word variables to integer variables, used inttostr to convert them to strings and then strtodatetime to get the desired datetime type. It works fine now!

Thnx
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top