首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> 更多程式語言 >> Delphi

Delphi 判斷時間差在範圍內:WithinPastYears/Months/Days... 系列

     編輯:Delphi

時間範圍檢查:八個 WithinPastXxx 函數判斷兩時間差是否在指定範圍

WithinPastYears/Months/Weeks/Days/Hours/Minutes/Seconds/MilliSeconds 判斷 ANow, AThen 之差是否 ≤ ACount 個單位。返回 Boolean,適用於「最近 N 天/小時」等業務邏輯。

函數列表

WithinPastYears        WithinPastMonths      WithinPastWeeks
WithinPastDays         WithinPastHours       WithinPastMinutes
WithinPastSeconds      WithinPastMilliSeconds

測試代碼 (基準:2009-5-20 11:22:33 vs 2009-5-21 11:22:33,差 1 天)

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。函數內部取絕對值比較,順序不影響結果。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved