function TryStrToDate(const S: string; out Value: TDateTime): Boolean; overload; function TryStrToDate(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean; overload;function TryStrToDateTime(const S: string; out Value: TDateTime): Boolean; overload; function TryStrToDateTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean; overload;
尝试进行转换,并捕捉异常。 如果没有发生异常,则说明能转换成日期型。例如: function CheckDateString(sTemp: String): Boolean; var dTemp : TDateTime; begin try dTemp := StrToDate(sTemp); Result := True; except; Result := False; end; end;procedure TForm1.Button1Click(Sender: TObject); var Temp : String; begin Temp := '2006-6-31'; if CheckDateString(Temp) then ShowMessage ('可以转换成日期型') else ShowMessage ('不可以转换成日期型'); end;
下面这条牛,判断YYYY-MM-DD这种格式的,基本上把闰年和2月等的情况都考虑进去了:^((((1[6-9]|[2-9]\d)\d{2})-(0?[13578]|1[02])-(0?[1-9]|[12]\d|3[01]))|(((1[6-9]|[2-9]\d)\d{2})-(0?[13456789]|1[012])-(0?[1-9]|[12]\d|30))|(((1[6-9]|[2-9]\d)\d{2})-0?2-(0?[1-9]|1\d|2[0-8]))|(((1[6-9]|[2-9]\d)(0[48]|[2468][048]|[13579][26])|((16|[2468][048]|[3579][26])00))-0?2-29-))$用于校验的:
^d{4}-(0?[1-9]|[1][012])-(0?[1-9]|[12][0-9]|[3][01])[s]+d([0-1][0-9]|2?[0-3]):([0-5][0-9]):([0-5][0-9])$
用于从文本提取的:
d{4}-(0?[1-9]|[1][012])-(0?[1-9]|[12][0-9]|[3][01])[s]+d([0-1][0-9]|2?[0-3]):([0-5][0-9]):([0-5][0-9])
其它:
^((\d{2}(([02468][048])|([13579][26]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|([1-2][0-9])))))|(\d{2}(([02468][1235679])|([13579][01345789]))[\-\/\s]?((((0?[13578])|(1[02]))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(3[01])))|(((0?[469])|(11))[\-\/\s]?((0?[1-9])|([1-2][0-9])|(30)))|(0?2[\-\/\s]?((0?[1-9])|(1[0-9])|(2[0-8]))))))(\s(((0?[1-9])|(1[0-2]))\:([0-5][0-9])((\s)|(\:([0-5][0-9])\s))([AM|PM|am|pm]{2,2})))?$
function TryStrToDate(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean; overload;function TryStrToDateTime(const S: string; out Value: TDateTime): Boolean; overload;
function TryStrToDateTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean; overload;
如果没有发生异常,则说明能转换成日期型。例如:
function CheckDateString(sTemp: String): Boolean;
var
dTemp : TDateTime;
begin
try
dTemp := StrToDate(sTemp);
Result := True;
except;
Result := False;
end;
end;procedure TForm1.Button1Click(Sender: TObject);
var
Temp : String;
begin
Temp := '2006-6-31';
if CheckDateString(Temp) then
ShowMessage ('可以转换成日期型')
else
ShowMessage ('不可以转换成日期型');
end;
temp:= '2006-06-31 ';
日期:=StrToDate(temp);
showmessage('可以转换');
except
showmessage('不可转换');
end;这个应该会用吧!
if(DateToStr(StrToDateDef(temp,"1900-01-01"))<>temp)
return false;
else
return true;
function TryStrToDate(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean; overload; function TryStrToDateTime(const S: string; out Value: TDateTime): Boolean; overload;
function TryStrToDateTime(const S: string; out Value: TDateTime; const FormatSettings: TFormatSettings): Boolean; overload;
这个比较好用
当然正则表达式也比较好,只是俺没有用过.