怎么检测某个串是否 可以转化成 日期型的?
例如  
temp:='2006-06-31';
用什么函数 检测  temp 是否能转化成日期型?

解决方案 »

  1.   

    关于验证日期的正则表达式简单的“年-月-日 小时:分钟:秒”形式的日期正则表达式为:^(\d{4})\-(\d{2})\-(\d{2}) (\d{2}):(\d{2}):(\d{2})$
    下面这条牛,判断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})))?$ 
      

  2.   

    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;
      

  3.   

    尝试进行转换,并捕捉异常。
    如果没有发生异常,则说明能转换成日期型。例如:
    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;
      

  4.   

    try
      temp:= '2006-06-31 '; 
      日期:=StrToDate(temp);
      showmessage('可以转换');
    except
      showmessage('不可转换');
    end;这个应该会用吧!
      

  5.   

    推荐 jadeluo 的方法。用正则表达式有点大材小用了 
      

  6.   

    从速度来讲还是推荐使用Try捕捉的方式来确定是否合法!
      

  7.   

    dulei115的正解,判断日期是否合法不用正则这么复杂吧...
      

  8.   

    to hongqi162 :太复杂了,,,看来try使用简单一下,,,哈哈,,,
      

  9.   

    我是这么用的:
    if(DateToStr(StrToDateDef(temp,"1900-01-01"))<>temp)
      return false;
    else
      return true;
      

  10.   

    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;
    这个比较好用
    当然正则表达式也比较好,只是俺没有用过.