Autor Beitrag
wazap
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 25



BeitragVerfasst: So 25.01.09 21:22 
Hallo.

Ich habe mir eine Function zusammen gebaut um die Differenz zwischen 2 TDate(s) aus zu rechnen.
Nun wollte ich Fragen ob das nich einfacher geht! Code hier (Ein paar sachen hab ich aus DateUtils entwendet um Unit unabhängig zu bleiben ^^):
ausblenden volle Höhe Delphi-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
41:
42:
43:
44:
45:
46:
47:
48:
49:
50:
function GetDateDifference(const Date1, Date2: TDate): string;
type
  PDayTable = ^TDayTable;
  TDayTable = array[1..12of Word;

const
  MonthDays: array [Boolean] of TDayTable = ((312831303130313130313031),
                                             (312931303130313130313031));
var
  D1, M1, Y1: Word;
  D2, M2, Y2: Word;
  D3, M3, Y3: Word;
begin
  if Date1 < Date2 then
  begin
    DecodeDate(Date1, Y1, M1, D1);
    DecodeDate(Date2, Y2, M2, D2);
  end
  else begin
    DecodeDate(Date1, Y1, M1, D1);
    DecodeDate(Date2, Y2, M2, D2);
  end;

  D3 := 0; M3 := 0; Y3 := 0;

  if Y1 <> Y2 then
  begin
    Y3 := Y2 - Y1;
    if M1 > M2 then Y3 := Y3 - 1;
  end;

  if M1 <> M2 then
  begin
    if M1 > M2 then
    begin
      M3 := (StrToInt('1' + IntToStr(M2))) - M1 + 1;

      if D1 < D2 then M3 := M3 + 1;
    end
    else M3 := M2 - M1;
  end;

  if D1 <> D2 then
  begin
    if D1 < D2 then D3 := D2 - D1
    else D3 := (MonthDays[(M3 = 2and (Y3 mod 4 = 0and ((Y3 mod 100 <> 0or (Y3 mod 400 = 0)), M3]) - D1 + 1;
  end;

  Result := Format('%d.%d.%d', [D3, M3, Y3]);
end;
PS: Eine Funktion in DP, Google und von CodeGear/Borland selber hab ich nicht gefunden.

Gruß wazap

[EDIT]Kleiner fehler behoben.


Zuletzt bearbeitet von wazap am So 25.01.09 21:28, insgesamt 1-mal bearbeitet
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: So 25.01.09 21:26 
ausblenden Delphi-Quelltext
1:
diff = Abs(date1 - date2);					


diff ist TDateTime;
Date1 und date 2 auch. ;-)

lg elundril

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
jaenicke
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starofftopic star
Beiträge: 19315
Erhaltene Danke: 1747

W11 x64 (Chrome, Edge)
Delphi 11 Pro, Oxygene, C# (VS 2022), JS/HTML, Java (NB), PHP, Lazarus
BeitragVerfasst: So 25.01.09 21:29 
Zudem gibts noch so nette Sachen wie DaysBetween usw. in der DateUtils.

Das Ergebnis der von user profile iconelundril genannten Differenz ist dann in Tagen.
wazap Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 25



BeitragVerfasst: So 25.01.09 21:31 
user profile iconelundril hat folgendes geschrieben Zum zitierten Posting springen:
ausblenden Delphi-Quelltext
1:
diff = Abs(date1 - date2);					


diff ist TDateTime;
Date1 und date 2 auch. ;-)

lg elundril
Meine Function Rechnet anderst: Differenze: "20.06.1993" & "25.01.2009" = 15 Jahre, 7 Monate, 5 Tage
delfiphan
ontopic starontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic star
Beiträge: 2684
Erhaltene Danke: 32



BeitragVerfasst: So 25.01.09 23:55 
user profile iconelundril hat folgendes geschrieben Zum zitierten Posting springen:
ausblenden Delphi-Quelltext
1:
diff = Abs(date1 - date2);					


diff ist TDateTime;
Date1 und date 2 auch. ;-)

lg elundril

Achtung das geht so nicht! TDateTime ist zwischen -1 und 0 nicht definiert, was beim Rechnen mit Daten vor 12/30/1899 speziell zu berücksichtigen ist.
elundril
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 3747
Erhaltene Danke: 123

Windows Vista, Ubuntu
Delphi 7 PE "Codename: Aurora", Eclipse Ganymede
BeitragVerfasst: Mo 26.01.09 10:24 
jep, habs gestern noch ausprobiert, kommt n mist raus, sorry.

_________________
This Signature-Space is intentionally left blank.
Bei Beschwerden, bitte den Beschwerdebutton (gekennzeichnet mit PN) verwenden.
Luckie
Ehemaliges Mitglied
Erhaltene Danke: 1



BeitragVerfasst: Mo 26.01.09 11:00 
user profile iconwazap hat folgendes geschrieben Zum zitierten Posting springen:
Meine Function Rechnet anderst: Differenze: "20.06.1993" & "25.01.2009" = 15 Jahre, 7 Monate, 5 Tage

Also die Formatierung der Ausgabe ist doch nebensächlich. Auch das Ergebnis von daysbetween kannst du nach wunsch formatieren; brauchst aber die ganze Rechnung nicht mehr selber machen und deine Funktion wird einfachher.