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

isDate() ? 2

Status
Not open for further replies.

jbpelletier

Programmer
Sep 8, 2001
232
CA
Do there is something, in delphi, to know if a string is a valid date.

im looking for something that looks like:

mydate:string;

if isdate(mydate) then .....

or

if mydate.isdate() then ....

jb
 
It is fairly easy to write a function that causes an exception if given an invalid date. If the exception is handled within the function it can set the result to false. For example:
Code:
function isDate ( const DateString: string ): boolean;
begin
  try
    StrToDate ( DateString );
    result := true;
  except
    result := false;
  end;
end;
 
Do a search in the help on DateUtils routines and you can find a list of a lot of DateTime functions. I'm pretty sure I've used one called IsValidDate(StrToDate(datestring)).

you have to use the StrToDate function as well since the IsValidDate takes a DateTime. But check the help to make sure.

Good luck! Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
The solution offered by Leslie is incorrect.

The IsValidDate function requires the date to be 3 words (year, month and day) and not a string as suggested.

It is therefore not a solution to the original problem.


Andrew





 
My bad! Here is what I do (I knew I used it to check a string though!)

Code:
function ConvertDate(ADate: string): string;
var
CheckDate : TDateTime;
Year, Month, Day : Word;
ValidDate : boolean;
begin
  CheckDate := StrToDate(ADate);
  DecodeDate(CheckDate, Year, Month, Day);
  ValidDate := IsValidDate(Year, Month, Day);
  If ValidDate Then
  Result := copy(d, 7, 4) + copy(d, 1, 2) + copy(d, 4, 2)
  else
  Result := '';
end;
Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
The problem with the code produced by Leslie is that if the date is invalid an EConvertError will occur on the line
Code:
  CheckDate := StrToDate(ADate);
and the function ConvertDate will terminate before even setting result to ' '.

The correct way to write the routine is as follows:
Code:
function ConvertDate(const ADate: string): string;
begin
  try
    StrToDate(ADate);
    Result := copy(ADate, 7, 4) + copy(ADate, 1, 2) + copy(ADate, 4, 2);
  except
    Result := ' ';
  end;
end;
Note that there is no need to assign the result of StrToDate to a variable because it is not used.

Andrew
 
Thanks Andrew! You get a star!
Leslie
landrews@metrocourt.state.nm.us

SELECT * FROM USERS WHERE CLUE > 0
No Rows Returned
 
Tanx a lot..

I was trying the "try" thing, but i was getting an error msg box. I realized that this error only occure when im executing my app via the "play" button (F9).

Do error msg box are always "skip" when the error occure in a "Try" block?

jb
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top