//判断用户输入的日期是否有效 function PValidDate(var strDate: String): Boolean; var curSep:Char; validSep:array [1..3] of Char; sDate,sYear,sMonth,sDay : string; iLen,iPos,i:integer; iYear, iMonth, iDay, iLastDay: Integer; const DaysInMonth: array[1..12] of Integer = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31); begin Result:=False; strDate:=Trim(StrDate); if strDate='' then exit; if Pos(' ',strDate)>0 then exit; validSep[1]:='-'; validSep[2]:='.'; validSep[3]:='/'; for i:=1 to 3 do if Pos(validSep[i],strDate)>0 then break; if i>3 then exit; curSep:=validSep[i]; iLen:=Length(strDate); iPos:=Pos(curSep,strDate); sYear:=Copy(strDate,1,iPos-1); i:=Length(sYear); if (i<>2) and (i<>4) then exit; if i=2 then if StrToInt(sYear)>70 then sYear:='19'+sYear else sYear:='20'+sYear; sDate:=Copy(strDate,iPos+1,iLen-iPos); iLen:=Length(sDate); iPos:=Pos(curSep,sDate); if iPos=0 then exit; sMonth:=Copy(sDate,1,iPos-1); if (Length(sMonth)>2) or (Length(sMonth)<1) then exit; if Length(sMonth)=1 then sMonth:='0'+sMOnth; sDay:=Copy(sDate,iPos+1,iLen-iPos); if (Length(sDay)>2) or (Length(sDay)<1) then exit; if Length(sDay)=1 then sDay:='0'+sDay; strDate:=sYear+sMonth+sDay; iYear := StrToInt( Copy( strDate, 1,4) ) ; iMonth := StrToInt( Copy( strDate, 5, 2 ) ); iDay := StrToInt( Copy(strDate, 7, 2) ); if (iYear<0) or (iMonth<1) or (iDay<1) then exit else begin iLastDay := DaysInMonth[ iMonth ] ; if (iYear mod 4 = 0) and ((iYear mod 100 <> 0) or (iYear mod 400 = 0)) and (iMonth = 2) then Inc(iLastDay); if (iMonth>12) or (iDay>iLastDay) then exit; end; Result:=True; strDate:=PFormatDate(strDate); end;
在delphi6中有IsDateTime这个函数
TryStrToDateTime() for Delphi6 TryStrToDate() for Delphi6
Suny_2001(小鱼)的真是挺简单,其实我也是这样想的,只不过我加一个用不到的变量,不知为什么就不报错,而是跳过去了。guasha(刮痧)的也很准。我由于用了Infopower for Delphi5不能用Delphi6,否则也不用这么麻烦了。多谢各位,很衷心地!!
function PValidDate(var strDate: String): Boolean;
var
curSep:Char;
validSep:array [1..3] of Char;
sDate,sYear,sMonth,sDay : string;
iLen,iPos,i:integer;
iYear, iMonth, iDay, iLastDay: Integer;
const
DaysInMonth: array[1..12] of Integer = (31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31);
begin
Result:=False;
strDate:=Trim(StrDate);
if strDate='' then exit;
if Pos(' ',strDate)>0 then exit;
validSep[1]:='-';
validSep[2]:='.';
validSep[3]:='/';
for i:=1 to 3 do
if Pos(validSep[i],strDate)>0 then break;
if i>3 then exit;
curSep:=validSep[i];
iLen:=Length(strDate);
iPos:=Pos(curSep,strDate);
sYear:=Copy(strDate,1,iPos-1);
i:=Length(sYear);
if (i<>2) and (i<>4) then exit;
if i=2 then
if StrToInt(sYear)>70 then
sYear:='19'+sYear
else
sYear:='20'+sYear;
sDate:=Copy(strDate,iPos+1,iLen-iPos);
iLen:=Length(sDate);
iPos:=Pos(curSep,sDate);
if iPos=0 then exit;
sMonth:=Copy(sDate,1,iPos-1);
if (Length(sMonth)>2) or (Length(sMonth)<1) then exit;
if Length(sMonth)=1 then sMonth:='0'+sMOnth;
sDay:=Copy(sDate,iPos+1,iLen-iPos);
if (Length(sDay)>2) or (Length(sDay)<1) then exit;
if Length(sDay)=1 then sDay:='0'+sDay;
strDate:=sYear+sMonth+sDay;
iYear := StrToInt( Copy( strDate, 1,4) ) ;
iMonth := StrToInt( Copy( strDate, 5, 2 ) );
iDay := StrToInt( Copy(strDate, 7, 2) );
if (iYear<0) or (iMonth<1) or (iDay<1) then
exit
else
begin
iLastDay := DaysInMonth[ iMonth ] ;
if (iYear mod 4 = 0) and ((iYear mod 100 <> 0) or (iYear mod 400 = 0)) and (iMonth = 2) then
Inc(iLastDay);
if (iMonth>12) or (iDay>iLastDay) then exit;
end;
Result:=True;
strDate:=PFormatDate(strDate);
end;
TryStrToDate() for Delphi6