连续的时间字符串转换为时间 

解决方案 »

  1.   

    源字符串格式为:20081024132532目标时间格式为:2008-10-24 13:25:32
    不用copy行吗?有没有直接一步到位的方法
      

  2.   

    1.函数:function lstrtodatetime(s: string): string;
    Const
    str:array[0..4] of string=('-','-',' ',':',':');
    strv:array[0..4] of integer=(5,8,11,14,17);
    var
    i:integer;
    begin
      for i :=0  to 4 do
       Insert(str[i], s, strv[i]);
      Result:=s;
    end;
    2:调用:procedure TForm1.Button1Click(Sender: TObject);
    var
    ss:string;
    begin
      ss:='20081024132532';
     edit1.Text:=lstrtodatetime(ss);
    end;
    3:只提供思路,其它还得靠自已.
      

  3.   

    你再稍改一下就是了
    function Mtstrtodate(value: string): TdateTime;  //字符 转日期型
    var 
      y, m, d, h, mm,ss: Word;
    begin 
      y:=StrToInt(Copy(Value, 1, 4));
      Delete(Value, 1, 4);
      m:=StrToInt(Copy(Value, 1, 2));
      Delete(Value, 1, 2);
      d:=StrToInt(Copy(Value, 1, 2));
      Delete(Value, 1, 2);
      h:=StrToInt(Copy(Value, 1, 2));
      Delete(Value, 1, 2);
      if Length(value) >  2 then
      begin
        mm := StrToInt(Copy(Value, 1, 2));
        ss := StrToInt(Copy(Value, 3, 2));
      end
      else
      begin
        mm := StrToInt(Value);
        ss := 0;
      end;  Result:=EncodeDate(y, m, d)+EncodeTime(h, mm, 0, 0) ;
    end;
      

  4.   

    var  
      S, EditMask: string;
      DateTime: TDateTime;
    begin
      S:= '20081024132532';
      EditMask:= '!99/99/00 99:99:99';
      DateTime:= StrToDateTime( MaskDoFormatText(EditMask, S, ' ') );
    end;