string1:='20080116180004';
目标字符窜(时间格式):'2008-01-16 18:00:04'

解决方案 »

  1.   

    写了一天程序后,脑细胞死了好多,不想去想了function TForm1.formatLikeDatetime(S:string):string;
    var
      str:string;
    begin
      str:=copy(S,1,4)+'-'+copy(S,5,2)+'-'
                    +copy(S,7,2)+' '+copy(S,9,2)+':'
                    +copy(S,11,2)+':'+copy(S,13,2);
      result:=str;
    end;
      

  2.   

    写个入门级的,呵呵function Tform1.formatstr(datestring:string):string;
    var
        year,month,day,hour,minute,second:string;
    begin
        if datestring='' then
            result:=''
        else
            try
            year:=copy(datestring,1,4);
            month:=copy(datestring,5,2);
            day:=copy(datestring,7,2);
            hour:=copy(datestring,9,2);
            minute:=copy(datestring,11,2);
            second:=copy(datestring,13,2);
            result:=year+'-'+month+'-'+day+' '+hour+':'+minute+':'+second;
            except
                application.MessageBox('格式化出错','提示',64);
            end;end;
      

  3.   

    这个怎么样:
    function GetFormatDateTime(const S: string): string;
    type
      TDTFormat = packed record
        Y: array [0..3] of Char;
        M: array [0..1] of Char;
        D: array [0..1] of Char;
        H: array [0..1] of Char;
        MS: array [0..1] of Char;
        S: array [0..1] of Char;
      end;
    var
      P: ^TDTFormat;  
    begin
      P := Pointer(S);
      if Length(S) >= SizeOf(TDTFormat) then
        Result := P.Y + '-' + P.M + '-' + P.D + ' ' + P.H + ':' + P.MS + ':' + P.S
      else
        Result := ''
    end;
      

  4.   

    我也凑个热闹,看这样写如何:
    procedure TForm1.Button1Click(Sender: TObject);
    var
      str: String;
    begin
      str := '20080116180004';
      insert('-',str,5);
      insert('-',str,8);
      insert(' ',str,11);
      insert(':',str,14);
      insert(':',str,17);
      showmessage(str);
    end;
      

  5.   

    4楼的不错,
    输出时用Format('%s-%s-%s %s:%s:%s',[P.Y,P.M,P.D,P.H,P.MS,P.S]);
      

  6.   

    先分配空间,再COPY。
    Format耗时不少的。
      

  7.   

    var
      string1,string2:String;
      p1,p2:PChar;
      iCount:Integer;
    begin
      string1:='20080116180004';
      SetLength(String2,19);
      p1:=Pchar(String1);
      p2:=PChar(String2);  //YYYY
      iCount := 4;
      move( p1^,p2^,iCount);
      inc(p1,iCount);
      inc(p2,iCount);
      //-
      p2^ := '-';
      inc(p2);  //MM
      iCount := 2;
      move( p1^,p2^,iCount);
      inc(p1,iCount);
      inc(p2,iCount);
      //-
      p2^ := '-';
      inc(p2);  //DD
      move( p1^,p2^,iCount);
      inc(p1,iCount);
      inc(p2,iCount);
      //(Space)
      p2^ := #$20;//
      inc(p2);  //hh
      move( p1^,p2^,iCount);
      inc(p1,iCount);
      inc(p2,iCount);
      //:
      p2^ := ':';//
      inc(p2);  //nn
      move( p1^,p2^,iCount);
      inc(p1,iCount);
      inc(p2,iCount);
      //:
      p2^ := ':';//
      inc(p2);  //ss
      move( p1^,p2^,iCount);
      ShowMessage(string2);
    end;