我原来写过一个判断 200201020304(精确到分钟)是否为日期格式的函数,参考一下
function TFrmYWXXCX.TimeIsTrue(sForm,sTo: string): boolean;
function dayInMonth(sYear,sMonth: integer): integer;
begin
case sMonth of
1,3,5,7,8,10,12:result := 31;
4,6,9,11:result := 30;
2:
if ((sYear mod 4 = 0) and (sYear mod 100 <> 0)) or ((sYear mod 100 = 0) and (sYear mod 400 = 0)) then
result := 29
else
result := 28;
end;
end;
function typeIsYes(s: string): boolean;
begin
result := true;
if length(s) <> 12 then result := false;
if (strtoint(copy(s,5,2)) > 12) or (strtoint(copy(s,5,2))<1) then result := false;
if (strtoint(copy(s,7,2)) > dayInMonth(strtoint(copy(s,1,4)),strtoint(copy(s,5,2)))) or (strtoint(copy(s,7,2))<1) then result := false;
if strtoint(copy(s,9,2)) > 23 then result := false;
if strtoint(copy(s,11,2)) > 59 then result := false;
end;
begin
result := true;
TimeForm := sForm;
TimeTo := sTo;
if sForm = '' then
begin
showerr('请输入起始时间!',2);
result := false;
exit;
end;
if (sForm <> '') and (sTo = '') then
begin
if typeIsYes(sForm) = false then
begin
result := false;
showerr('时间格式出错,请参考提示的格式',2);
end
else begin
timeTo := FormatDateTime('yyyymmddhhmm',now);
sTo := timeTo;
end;
end
else
begin
if not(typeIsYes(sForm) and typeIsYes(sTo)) then
begin
result := false;
showerr('时间格式出错,请参考提示的格式',2);
end;
if sTo < sForm then
begin
result := false;
showerr('结束时间必须不能小于开始时间!',2);
end;
end;
end;