StrToDateTime这个函数只能把符合“locale’s date/time format”的字符串变成datetime,现在我有一个字符串,不符合这个格式,但是符合其他时间格式,怎么变成时间?比如说现在我可以把5/19/2003 7:57:26 PM通过strtodatetime变成tdatetime类型,但是我有一个Jun 3 2003 03:04这种格式的字符串。怎么变成tdatetime类型?有没有别的函数。

解决方案 »

  1.   

    idilent (说错了别怪我)  :太小气了吧?才20分?呵呵,用笨办法我会。估计不是你想要的,所以不说。**********************************************
    *等我有钱了,我就站在天安门广场挨个给大家发钱*
    **********************************************
      

  2.   

    试一下TryStrToDatetime()支持很多格式
      

  3.   

    StrToDateTime(5/19/2003 7:57:26 PM)一样错误
      

  4.   

    to microchu(扬帆) 因为你的机器的时间格式也不是这种。strtodatetime只能转变与机器时间格式一致的字符串。
      

  5.   

    To idilent (说错了别怪我) :
      短消息已收到,我用的是D7,代码在SysUtils.Pas 中,相关函数如下
    function TryStrToDateTime(const S: string; out Value: TDateTime): Boolean;
    var
      Pos: Integer;
      Date, Time: TDateTime;
    begin
      Result := True;
      Pos := 1;
      Time := 0;
      if not ScanDate(S, Pos, Date) or
         not ((Pos > Length(S)) or ScanTime(S, Pos, Time)) then    // Try time only
        Result := TryStrToTime(S, Value)
      else
        if Date >= 0 then
          Value := Date + Time
        else
          Value := Date - Time;
    end;function ScanDate(const S: string; var Pos: Integer; var Date: TDateTime;
      const FormatSettings: TFormatSettings): Boolean; overload;
    var
      DateOrder: TDateOrder;
      N1, N2, N3, Y, M, D: Word;
      L1, L2, L3, YearLen: Byte;
      CenturyBase: Integer;
      EraName : string;
      EraYearOffset: Integer;  function EraToYear(Year: Integer): Integer;
      begin
    {$IFDEF MSWINDOWS}
        if SysLocale.PriLangID = LANG_KOREAN then
        begin
          if Year <= 99 then
            Inc(Year, (CurrentYear + Abs(EraYearOffset)) div 100 * 100);
          if EraYearOffset > 0 then
            EraYearOffset := -EraYearOffset;
        end
        else
          Dec(EraYearOffset);
    {$ENDIF}
        Result := Year + EraYearOffset;
      end;begin
      Y := 0;
      M := 0;
      D := 0;
      YearLen := 0;
      Result := False;
      DateOrder := GetDateOrder(FormatSettings.ShortDateFormat);
      EraYearOffset := 0;
      if FormatSettings.ShortDateFormat[1] = 'g' then  // skip over prefix text
      begin
        ScanToNumber(S, Pos);
        EraName := Trim(Copy(S, 1, Pos-1));
        EraYearOffset := GetEraYearOffset(EraName);
      end
      else
        if AnsiPos('e', FormatSettings.ShortDateFormat) > 0 then
          EraYearOffset := EraYearOffsets[1];
      if not (ScanNumber(S, Pos, N1, L1) and ScanChar(S, Pos, FormatSettings.DateSeparator) and
        ScanNumber(S, Pos, N2, L2)) then Exit;
      if ScanChar(S, Pos, FormatSettings.DateSeparator) then
      begin
        if not ScanNumber(S, Pos, N3, L3) then Exit;
        case DateOrder of
          doMDY: begin Y := N3; YearLen := L3; M := N1; D := N2; end;
          doDMY: begin Y := N3; YearLen := L3; M := N2; D := N1; end;
          doYMD: begin Y := N1; YearLen := L1; M := N2; D := N3; end;
        end;
        if EraYearOffset > 0 then
          Y := EraToYear(Y)
        else
        if (YearLen <= 2) then
        begin
          CenturyBase := CurrentYear - FormatSettings.TwoDigitYearCenturyWindow;
          Inc(Y, CenturyBase div 100 * 100);
          if (FormatSettings.TwoDigitYearCenturyWindow > 0) and (Y < CenturyBase) then
            Inc(Y, 100);
        end;
      end else
      begin
        Y := CurrentYear;
        if DateOrder = doDMY then
        begin
          D := N1; M := N2;
        end else
        begin
          M := N1; D := N2;
        end;
      end;
      ScanChar(S, Pos, FormatSettings.DateSeparator);
      ScanBlanks(S, Pos);
      if SysLocale.FarEast and (System.Pos('ddd', FormatSettings.ShortDateFormat) <> 0) then
      begin     // ignore trailing text
        if FormatSettings.ShortTimeFormat[1] in ['0'..'9'] then  // stop at time digit
          ScanToNumber(S, Pos)
        else  // stop at time prefix
          repeat
            while (Pos <= Length(S)) and (S[Pos] <> ' ') do Inc(Pos);
            ScanBlanks(S, Pos);
          until (Pos > Length(S)) or
            (AnsiCompareText(FormatSettings.TimeAMString,
             Copy(S, Pos, Length(FormatSettings.TimeAMString))) = 0) or
            (AnsiCompareText(FormatSettings.TimePMString,
             Copy(S, Pos, Length(FormatSettings.TimePMString))) = 0);
      end;
      Result := TryEncodeDate(Y, M, D, Date);
    end;function TryStrToTime(const S: string; out Value: TDateTime): Boolean;
    var
      Pos: Integer;
    begin
      Pos := 1;
      Result := ScanTime(S, Pos, Value) and (Pos > Length(S));
    end;
    function ScanTime(const S: string; var Pos: Integer;
      var Time: TDateTime): Boolean; overload;
    var
      BaseHour: Integer;
      Hour, Min, Sec, MSec: Word;
      Junk: Byte;
    begin
      Result := False;
      BaseHour := -1;
      if ScanString(S, Pos, TimeAMString) or ScanString(S, Pos, 'AM') then
        BaseHour := 0
      else if ScanString(S, Pos, TimePMString) or ScanString(S, Pos, 'PM') then
        BaseHour := 12;
      if BaseHour >= 0 then ScanBlanks(S, Pos);
      if not ScanNumber(S, Pos, Hour, Junk) then Exit;
      Min := 0;
      Sec := 0;
      MSec := 0;
      if ScanChar(S, Pos, TimeSeparator) then
      begin
        if not ScanNumber(S, Pos, Min, Junk) then Exit;
        if ScanChar(S, Pos, TimeSeparator) then
        begin
          if not ScanNumber(S, Pos, Sec, Junk) then Exit;
          if ScanChar(S, Pos, DecimalSeparator) then
            if not ScanNumber(S, Pos, MSec, Junk) then Exit;
        end;
      end;
      if BaseHour < 0 then
        if ScanString(S, Pos, TimeAMString) or ScanString(S, Pos, 'AM') then
          BaseHour := 0
        else
          if ScanString(S, Pos, TimePMString) or ScanString(S, Pos, 'PM') then
            BaseHour := 12;
      if BaseHour >= 0 then
      begin
        if (Hour = 0) or (Hour > 12) then Exit;
        if Hour = 12 then Hour := 0;
        Inc(Hour, BaseHour);
      end;
      ScanBlanks(S, Pos);
      Result := TryEncodeTime(Hour, Min, Sec, MSec, Time);
    end;
      

  6.   

    可能不全,需要的话我可以mail给你