Einzelnen Beitrag anzeigen

alzaimar
(Moderator)

Registriert seit: 6. Mai 2005
Ort: Berlin
4.956 Beiträge
 
Delphi 2007 Enterprise
 
#6

Re: Genauer(!) Unterschied von zwei TDateTimes

  Alt 6. Jun 2009, 07:18
Hab auch einen:
Delphi-Quellcode:
uses
  SysUtils, DateUtils, Math;

Type
  TDateDiff = (dfYears, dfMonths, dfDays, dfHours, dfMinutes, dfSeconds);

Const
  sDateDiff : Array [TDateDiff] Of String =
    ('Jahre','Monate','Tage','Stunden','Minuten','Sekunden');

Function DateDiffStr (aDateTime1, aDateTime2 : TDateTime) : String;
Var
  d : TDateDiff;
  n : Integer;
  days : Extended;

  Function _DaysInSection (aDateDiff : TDateDiff) : Extended;
  Begin
    case aDateDiff of
      dfYears:
        Result := DateUtils.DaysInYear(aDateTime1);
      dfMonths:
        Result := DateUtils.DaysInMonth(aDateTime1);
      dfDays:
        Result := DateUtils;
      dfHours:
        Result := DateUtils.OneHour;
      dfMinutes:
        Result := DateUtils.OneMinute;
      dfSeconds:
        Result := DateUtils.OneSecond;
    end;
  End;

Begin
  aDateTime2 := aDateTime2 + 1/(2*DateUtils.OneSecond);
  Result := '';
  For d:=Low (TDateDiff) To High (TDateDiff) Do Begin
    n := 0;
    days := _DaysInSection(d);
    While days + aDateTime1 <= aDateTime2 Do Begin
      inc(n);
      aDateTime1 := aDateTime1 + days;
    days := _DaysInSection(d);
    End;
    If n>0 Then
      Result := Format ('%s %d %s ',[Result, n, sDateDiff[d]]);
  End;
End;
"Wenn ist das Nunstruck git und Slotermeyer? Ja! Beiherhund das Oder die Flipperwaldt gersput!"
(Monty Python "Joke Warefare")
  Mit Zitat antworten Zitat