function YearsBetween(const ANow, AThen: TDateTime): Integer;//返回两个日期间隔的年数
function MonthsBetween(const ANow, AThen: TDateTime): Integer;//返回两个日期间隔的月数
function WeeksBetween(const ANow, AThen: TDateTime): Integer;//返回两个日期间隔的星期数
function DaysBetween(const ANow, AThen: TDateTime): Integer;//返回两个日期间隔的天数
function HoursBetween(const ANow, AThen: TDateTime): Int64;//返回两个日期间隔的小时数
function MinutesBetween(const ANow, AThen: TDateTime): Int64;//返回两个日期间隔的分钟数
function SecondsBetween(const ANow, AThen: TDateTime): Int64;//返回两个日期间隔的秒数
function MilliSecondsBetween(const ANow, AThen: TDateTime): Int64;//返回两个日期间隔的毫秒数
1.
2.
3.
4.
5.
6.
7.
8.
单元System.DateUtils还提供了其他大量实用的日期类型的函数,感兴趣可以参考。
现在实现时间差大于多少秒的函数:
登录后复制
unit Unit2;
interface
uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Objects,
FMX.Controls.Presentation, FMX.StdCtrls,system.dateutils;
type
TForm2 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
function CompareSeconds(ANow,AThen:TDateTime;aSec:Integer):Boolean;
end;
var
Form2: TForm2;
implementation
{$R *.fmx}
procedure TForm2.Button1Click(Sender: TObject);
var
d1,d2:TDateTime;
d:double;
begin
d1:=now;
d2:=now-1;
// d:=(d1-d2)*24*60*60;//将时间差转换成秒数
if CompareSeconds(d1,d2,60) then
ShowMessage('ok');
end;
function TForm2.CompareSeconds(ANow, AThen: TDateTime; aSec: Integer): Boolean;
begin
result:= SecondsBetween(ANow,AThen)>=aSec;
end;
end.
再次强调,Delphi为我们做了大量处理日期的函数,涉及到这方面的代码,一定不要自己造轮子,直接去System.DateUtils单元去找找。
-----------------------------------
https://blog.51cto.com/kinglandsoft/3224956
Copyright © 2014 DelphiW.com 开发 源码 文档 技巧 All Rights Reserved
晋ICP备14006235号-8 晋公网安备 14108102000087号
执行时间: 0.04587197303772 seconds