Thema: Delphi Datumskonvertierung

Einzelnen Beitrag anzeigen

Benutzerbild von mschaefer
mschaefer

Registriert seit: 4. Feb 2003
Ort: Hannover
2.029 Beiträge
 
Delphi XE3 Enterprise
 
#10

TimeStr im Format 'hh:mm:ss' in DateTime

  Alt 12. Jan 2017, 20:41
Hier könnte auch einer meiner Routinen noch hin passen. Habe Zeitangaben in
Editfeldern die vom String in ein DateTime Format gewandelt werden*sollen.

Delphi-Quellcode:
   // Wandelt einen TimeStr im Format 'hh:mm:ss' in ein DateTime von heute
   function TimeStrToDateTime( InStr : String ) : TDateTime;
   var I ,j : integer;
       LInstr : String;
       th,tm,ts : String;
       fs : TFormatSettings;
   begin
      // Zahlenbereiche auf Ziffern prüfen //
      j := 1; th :=''; tm :=''; ts:='';
      for i := 1 to Length(InStr) do
      begin
            If ( ((ord(InStr[i]) >= 48) and (ord(InStr[i]) <= 57 )) ) then
            begin
            case j of
             1 : th:=th+InStr[i];
             2 : tm:=tm+InStr[i];
             3 : ts:=ts+InStr[i];
            end;
            end else
            begin
               if InStr[i] <> #32 then j := j +1;
            end;
      end;

      // Mit aktuellem Datum zusammenführen im Format: 14.10.2003, 10:26:05
      LInstr := copy( FormatDateTime('dd.mm.yyyy hh:nn:ss', now) ,1,11 )
              + th + ':'
              + tm + ':'
              + ts ;
      LInstr := trim(LInstr);

      // Datums Zeit format festlegen
      fs.DateSeparator := '.';
      fs.TimeSeparator := ':';
      fs.ShortDateFormat := 'dd.mm.yyyy hh:nn:ss';

      // String in DatumsZeitFormat wandeln
      try
         Result := StrToDateTime( LInstr, fs );
      except
         Result := StrToDateTime('01.01.2000 00:00:01', fs );
      end;
   end;

begin

  showmessage( DateTimeToStr( TimeStrToDateTime( '15:30:10' )));

end;
Grüße // Martin
Martin Schaefer
Phaeno
  Mit Zitat antworten Zitat