Thema: Zeitcode

Einzelnen Beitrag anzeigen

Benutzerbild von KodeZwerg
KodeZwerg

Registriert seit: 1. Feb 2018
3.685 Beiträge
 
Delphi 11 Alexandria
 
#21

AW: Zeitcode

  Alt 25. Jul 2018, 18:29
FormatSettings hat gefehlt, so klappt es, ich habe es getestet mit Delphi 2009.
Delphi-Quellcode:
function GetMillisecondSpan(S1, S2: String): Int64;
var
  t1, t2: TDateTime;
  FS: TFormatSettings;
begin
  GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, FS);
  FS.DecimalSeparator := '.';
  FS.TimeSeparator := ':';
  FS.LongTimeFormat := 'hh:nn:ss.zzz';
  try
    t1 := StrToTime(S1, FS);
    t2 := StrToTime(S2, FS);
  finally
    Result := MilliSecondsBetween(t1, t2);
  end;
end;

procedure TForm1.DurationClick(Sender: TObject);
begin
  Duration.Text := IntToStr(GetMillisecondSpan('01:23:45.678', '01:23:45.901'));
end;
In diesem Beispiel ist Duration.Text = 223

Delphi-Quellcode:
function GetDateTimeSpan ( S1, S2: String ): TDateTime;
var
  t1, t2: TDateTime;
  FS: TFormatSettings;
begin
  GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, FS);
  FS.DecimalSeparator := '.';
  FS.TimeSeparator := ':';
  FS.LongTimeFormat := 'hh:nn:ss.zzz';
  try
    t1 := StrToTime(S1, FS);
    t2 := StrToTime(S2, FS);
  finally
    Result := MilliSecondsBetween(t1, t2) / (1000.0 * 86400);
  end;
end;

procedure TForm1.DurationClick(Sender: TObject);
var
  FS: TFormatSettings;
begin
  GetLocaleFormatSettings(LOCALE_SYSTEM_DEFAULT, FS);
  FS.DecimalSeparator := '.';
  FS.TimeSeparator := ':';
  FS.LongTimeFormat := 'hh:nn:ss.zzz';
  Duration.Text := TimeToStr(GetDateTimeSpan('01:23:45.678','01:23:45.901'), FS);
end;
In diesem Beispiel ist Duration.Text = 00:00:00.223

Ich hoffe nun ist dieser Thread zufriedenstellend erledigt.
Gruß vom KodeZwerg

Geändert von KodeZwerg (25. Jul 2018 um 20:00 Uhr)
  Mit Zitat antworten Zitat