Thema: Delphi Datum letzter Samstag?

Einzelnen Beitrag anzeigen

Benutzerbild von Sir Rufo
Sir Rufo

Registriert seit: 5. Jan 2005
Ort: Stadthagen
9.454 Beiträge
 
Delphi 10 Seattle Enterprise
 
#35

AW: Datum letzter Samstag?

  Alt 24. Jul 2014, 09:58
Noch ein Vorschlag ohne Schleife.

Delphi-Quellcode:
Function LastDow (const DateToMatch : TDateTime; const DowToMatch : Byte) : TDateTime;
begin
   result := DateToMatch - ((7 - DowToMatch + DayOfTheWeek(DateToMatch)) mod 7);
   if result = DateToMatch then
      result := result - 7;
end;
LastDow(Date, DayFriday) sollte dann den 18.7. ergeben.
Kleine Verbesserung
Delphi-Quellcode:
Function LastDow (const DateToMatch : TDateTime; const DowToMatch : Byte) : TDateTime;
begin
  result := ( DateToMatch - 1 ) - ((DaysPerWeek - DowToMatch + DayOfTheWeek( DateToMatch - 1 )) mod DaysPerWeek);
end;
Kaum macht man's richtig - schon funktioniert's
Zertifikat: Sir Rufo (Fingerprint: ‎ea 0a 4c 14 0d b6 3a a4 c1 c5 b9 dc 90 9d f0 e9 de 13 da 60)
  Mit Zitat antworten Zitat