WithinPastYears/Months/Weeks/Days/Hours/Minutes/Seconds/MilliSeconds 判斷 ANow, AThen 之差是否 ≤ ACount 個單位。返回 Boolean,適用於「最近 N 天/小時」等業務邏輯。
WithinPastYears WithinPastMonths WithinPastWeeks
WithinPastDays WithinPastHours WithinPastMinutes
WithinPastSeconds WithinPastMilliSeconds
uses DateUtils;
procedure TForm1.FormCreate(Sender: TObject);
var t1, t2: TDateTime; b: Boolean;
begin
t1 := StrToDateTime('2009-5-20 11:22:33');
t2 := StrToDateTime('2009-5-21 11:22:33');
b := WithinPastYears(t1, t2, 24); {True: 1 天 < 24 年}
b := WithinPastMonths(t1, t2, 24); {True: 1 天 < 24 月}
b := WithinPastWeeks(t1, t2, 24); {True: 1 天 < 24 周}
b := WithinPastDays(t1, t2, 24); {True: 1 天 ≤ 24 天}
b := WithinPastHours(t1, t2, 24); {True: 24 小時 ≤ 24 小時}
b := WithinPastMinutes(t1, t2, 24); {False: 1440 分鐘 > 24}
b := WithinPastSeconds(t1, t2, 24); {False}
b := WithinPastMilliSeconds(t1, t2, 24);{False}
end;
注意: 參數順序為 (ANow, AThen, ACount),通常 ANow >= AThen。函數內部取絕對值比較,順序不影響結果。