Delphi中判断字符串是否符合⽇期格式Function TF_tn.IsDate(VarString:string): Boolean;
Var Sinput,smon,sday:String ; iyear,imon,iday:integer;
Begin
Sinput:=VarString;
if (length(sinput)>10)or(Length(Sinput)<8)or(Copy(sinput,5,1)<>'-') then
Begin
IsDate:=False;
Exit;
End;
if IsDigital(Copy(Sinput,1,4))=False then
Begin
IsDate:=False;
Exit;
End;
if length(Sinput)=8 then
Begin
if IsDigital(Copy(sinput,6,1)) and (copy(Sinput,7,1)='-') and IsDigital(Sinput[1])
then
Begin
smon:=sinput[6];
sday:=sinput[8];
End
else
Begin
IsDate:=False;
Exit;
End;
end
else
if Length(Sinput)=9 then
Begin
if IsDigital(copy(Sinput,6,1)) and IsDigital(copy(Sinput,8,2)) and (Sinput[7]='-') then Begin
smon:=copy(Sinput,6,1);
sday:=copy(Sinput,8,2);
End
else if IsDigital(copy(Sinput,6,2)) and IsDigital(copy(Sinput,9,1)) and (Sinput[8]='-') then Begin
smon:=copy(Sinput,6,2);
sday:=copy(Sinput,9,1);
End
Else
Begin
IsDate:=False;
Exit;
End;
End
else
if Length(Sinput)=10 then
Begin
if IsDigital(copy(Sinput,6,2)) and IsDigital(copy(Sinput,9,2)) and (Sinput[8]='-') then Begin
smon:=copy(Sinput,6,2);
sday:=copy(Sinput,9,2);
End
Else
Begin
IsDate:=False;
End;
End
else
Begin
Isdate:=False;
Exit;
End;
imon:=StrToInt(smon);
iday:=StrToInt(sday);
if (imon<1) or (imon>12) then
Begin
Isdate:=False;
Exit;
End;
//判断⼀个年份是不是闰年,以决定2⽉份是28天,还是29天
iyear:=StrToInt(Copy(Sinput,1,4));
if imon=2 then
Begin
if Frac(iyear/4)=0 then
Begin
if (iday>0) and (iday<30) then
Begin
isDate:=True;
Exit;
End
Else
Begin
isDate:=False;
Exit;
End;
End
字符串截取日期Else
Begin
if (iday>0) and (iday<29) then
Begin
isDate:=True;
Exit;
End
Else
Begin
isDate:=False;
Exit;
End;
End;
End
else
if (imon=1) or (imon=3) or(imon=5) or(imon=7) or (imon=8) or (imon=10) or(imon=12) then BEgin
if (iday>0) and (iday<32) then
Begin
Isdate:=True;
Exit;
End
Else
Begin
IsDate:=False;
Exit;
End;
End
else if (imon=4) or (imon=6) or(imon=9) or(imon=11) then
if (iday>0) and (iday< 31) then
Begin
Isdate:=True; Exit;
End
Else
Begin
IsDate:=False; Exit;
End;
End;
isDate:=False; End;

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。