1)
var
  fs: TFormatSettings;
begin
  fs.DateSeparator := Char('/');
  fs.TimeSeparator := Char(':');
  fs.ShortDateFormat := 'yyyy/mm/dd hh:nn:ss';
  ShowMessage('"' + DateTimeToStr(Now, fs) + '"');
end;
  用上面的代码显示的日期字符后面怎么有一个空格?怎么去掉它(不要用Trim())2)
var
  s: String;
  sl: TStringList;
begin
  s := '5154,11,984,2004/12/09 12:20:30,24';
  sl := TStringList.Create;
  sl.Delimiter := ',';
  sl.DelimitedText := s;
  ShowMessage(IntToStr(sl.Count));
end;
  用上面代码分开字符串,结果Count=6,它把'2004/12/09 12:20:30'拆成'2004/12/09'和'12:20:30'两个字符串,但我需要的是连在一起的字符,怎么写?

解决方案 »

  1.   

    1)
    用FormatDateTime('yyyy-mm-dd hh:nn:ss', Now);2)
    function SplitString(const Source: String; Reg: Char): TStrings;
    var
      i: Integer;
      sStrings: TStrings;
      sTmp: String;
    begin
      Result := nil;
      if Source <> '' then
      begin
        i := 1;
        sTmp := '';
        sStrings := TStringList.Create;
        while i<=Length(Source) do
        begin
          if (Source[i] = Reg) or (i = Length(Source)) then
          begin
            if (i = Length(Source)) and (Source[i] <> Reg) then
              sTmp := sTmp + Source[i];
            Inc(i);
            sStrings.Add(sTmp);
            sTmp := '';
          end
          else
          begin
            sTmp := sTmp + Source[i];
            Inc(i);
          end;
        end;
        Result := sStrings;
      end;
    end;
      

  2.   

    1)不能用FormatDateTime,因为我要这个字符串保存在文件,然后从文件读出再用StrToDateTime(s, fs)转换成TDateTime。
    2)太复杂了一些,有没有简单一点的。
      

  3.   

    你按照FormatDateTime转换成的字符串,可以直接用StrToDateTime转换成TDateTime格式的!procedure TForm1.FormCreate(Sender: TObject);
    begin
      Edit1.Text := FormatDateTime('yyyy-mm-dd hh:nn:ss', Now);
    end;procedure TForm1.Button1Click(Sender: TObject);
    var
      DateTime: TDateTime;
    begin
      DateTime := StrToDateTime(Edit1.Text);
      DateTimePicker1.Date := DateTime;
      DateTimePicker2.Time := DateTime;
    end;2)就是一个分割字符串的函数,直接就可以用!
      

  4.   


    用 FormatDateTime和StrToDateTime两个函数.
      

  5.   

    RE: gobiz(拔剑容易收剑难)和chinaandys(天煞孤星&&蛋炒饭)
    StrToDateTime(string)这个函数跟系统的区域设置有关,如果区域设置的格式是'yyyy/M/d',那么用StrToDateTime('2004-12-09 12:20:30')转换的话会出错。不可能让客户的机器上的区域设置跟自己的机器上一致。
      

  6.   

    String a;
     a:=DateTimeToStr(Now, fs);
        a:=leftstr(a,length(a)-1);
    这样不行?
      

  7.   

    2)给出这个函数 
      function FastSplit(const S : String; const Delimiter : String=',') : TStrings;
    var
      i,j : integer;
    begin
      result := TStringList.Create;
      i := 1;
      j := Pos(Delimiter,S);
      While j > 0 do
      begin
        result.Add(Copy(S,i,j-i));
        i := j + 1;
        j := PosEx(Delimiter,S,i);
      end;
      result.Add(Copy(S,i,Length(S)));
    end;
      

  8.   

    谢谢 liunini(妮妮) 
    第一个问题,我自己解决了:
     fs.ShortDateFormat := 'yyyy/mm/dd hh:nn:ss';
    改为:
        fs.ShortDateFormat := 'yyyy/mm/dd';
        fs.LongTimeFormat := 'hh:nn:ss';
    DateTimeToStr()函数转换时日期使用ShortDateFormat,时间使用LongTimeFormat
    不用TFormatSettings的话,也可用同名全局变量.第二个当然用liunini(妮妮) 的了. :)