//判断字符串是否是有效日期类型 function isdate(s:string):boolean; begin result:=false; try strtodate(s); result:=true; except on econverterror do result:=false; end; end;
why? function isdate(s:string):boolean; begin result:=false; try strtodate(s); result:=true; except on econverterror do result:=false; end; end; procedure TForm1.Button1Click(Sender: TObject); begin if isdate('2004-9-31') then showmessage('是') else showmessage('否'); end;日期分隔符换成“-”试下。 自己写代码判断,麻烦。
to function is function isdate(s:string):boolean; begin result:=true; try strtodate(s); except on econverterror do result:=false; end; end;
一个日期? 楼主的意思是字符串还是TDateTime?
function IsRightDate(mInputDate: String): Boolean; begin try StrToDate(mInputDate); Result:=True; except Result:=False; end; end;
你把年月日分解出来,然后用下面的函数,如果无效则返回False。function TryEncodeDate(Year, Month, Day: Word; out Date: TDateTime): Boolean;
function isdate(s:string):boolean; begin result:=false; try strtodate(s); result:=true; except on e:exception do begin result:=false; showmessage(e.Message); end; end; end;
DELPHI中也能有类似这样的函数.
function isdate(s:string):boolean;
begin
result:=false;
try
strtodate(s);
result:=true;
except
on econverterror do
result:=false;
end;
end;
Except不哦自己写字符串分析都能OK的http://lysoft.7u7.net
用try/except可以实现,不过调试代码时会异常,很烦
楼上的两位,你们说的Try
Except也不行,这个异常无法捕捉
比如2004.03.03你把小数点去掉..换成20040303是FORMATDATE('YYYYDDMM'),可以设置程序日期都为那种格式的
function isdate(s:string):boolean;
begin
result:=false;
try
strtodate(s);
result:=true;
except
on econverterror do
result:=false;
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
if isdate('2004-9-31') then
showmessage('是')
else
showmessage('否');
end;日期分隔符换成“-”试下。
自己写代码判断,麻烦。
function isdate(s:string):boolean;
begin
result:=true;
try
strtodate(s);
except
on econverterror do
result:=false;
end;
end;
begin
try
StrToDate(mInputDate);
Result:=True;
except
Result:=False;
end;
end;
begin
result:=false;
try
strtodate(s);
result:=true;
except
on e:exception do
begin
result:=false;
showmessage(e.Message);
end;
end;
end;