XE2 日期字符串转日期的函数

//可识别处理多种格式.


Function myStrToDateTime(sdate:String):TDateTime;

const
  dSeparator = '-';
var
  dtpos,tmpos:Integer;
begin
  result := 0;
  FormatSettings.DateSeparator := dSeparator;
  FormatSettings.TimeSeparator := ':';
  FormatSettings.ThousandSeparator := ',';
  FormatSettings.ShortDateFormat :='yyyy-mm-dd';
  FormatSettings.ShortTimeFormat :='hh:nn:ss';
  FormatSettings.LongDateFormat :='yyyy-mm-dd';
  FormatSettings.LongTimeFormat :='hh:nn:ss';
  sdate := ReplaceStr( sdate,'.' ,dSeparator);
  sdate := ReplaceStr( sdate,'/' ,dSeparator);
  sdate := ReplaceStr( sdate,'\' ,dSeparator);
  sdate := ReplaceStr( sdate,'年',dSeparator);
  sdate := ReplaceStr( sdate,'月',dSeparator);
  sdate := ReplaceStr( sdate,'日',dSeparator);
  dtpos := Pos(dSeparator,sdate);
  tmpos := Pos(':',sdate);
  //yyyymmdd字符串格式转日期.
  if (dtpos=0) and (tmpos=0) and (length(sdate)=8) then
  begin
    sdate := Copy(sdate,1,4)+ dSeparator + Copy(sdate,5,2)+ dSeparator + Copy(sdate,7,2);
  end;
  //yyyymmdd hh:nn:ss字符串格式日期.
  if (dtpos=0) and (tmpos>0) and (length(sdate)>13) then
  begin
    tmpos := Pos(' ',sdate);
    sdate := Copy(sdate,1,4)+ dSeparator + Copy(sdate,5,2)+ dSeparator + Copy(sdate,7,2) + Copy(sdate,tmpos,8);
  end;
  result := StrToDateTimeDef(sdate, 0, FormatSettings);

end;


你可能感兴趣的:(RAD,Studio,XE)