比如说我用当前系统时间减去一个时间,怎样可以得到它们之间的相差天数,小时数,分钟数。三个数据要分开得到。要怎样才能实现?
解决方案 »
- 100元购买delphi socket服务器代码,要求支持500个客户端,30个并发连接
- TIdTCPClient的联接问题
- 怎么搞的,delphi用着怎么这么不顺呀??
- 怎样在delphi 中调用帮助文件,急急!!!在线等待
- 关于ADO中的存储过程!...SOS...SOS...SOS...SOS...SOS...SOS...SOS...SOS...
- 还是那个图像问题-再线急等
- 帮帮忙,解释一下这段程序
- 局部择优搜索和全局择优搜索
- 运算器 (((3+5)/2-6)×5)^5
- Project->Options->Forms下的Auto-create forms和Availabel Forms有何區別
- 如何实现coreldraw中的复制的内容显示在delphi的Image控件中?
- listview 的语法
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, ComCtrls, StdCtrls;type
TForm1 = class(TForm)
btn1: TButton;
dtp1: TDateTimePicker;
dtp2: TDateTimePicker;
lbl1: TLabel;
procedure btn1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;var
Form1: TForm1;implementationuses DateUtils;{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);
var
aDateTime, aInterval: TDateTime;
begin
aDateTime := EncodeDateTime(YearOf(dtp1.Date), MonthOf(dtp1.Date), DayOf(dtp1.Date)
, HourOf(dtp2.Time), MinuteOf(dtp2.Time), 0, 0); aInterval := MinuteSpan(Now, aDateTime);
lbl1.Caption := Format('相差天数%d,小时数%d,分钟数%d'
, [
Trunc(aInterval /60/24), Trunc(aInterval / 60), Trunc(aInterval) mod 60
]);
end;end.
2.75 1/1/1900 6:00 pm
-1.25 12/29/1899 6:00 am
35065 1/1/1996 12:00 am
To find the fractional number of days between two dates, simply subtract the two values, unless one of the TDateTime values is negative. Similarly, to increment a date and time value by a certain fractional number of days, add the fractional number to the date and time value if the TDateTime value is positive.
When working with negative TDateTime values, computations must handle time portion separately. The fractional part reflects the fraction of a 24-hour day without regard to the sign of the TDateTime value. For example, 6:00 am on 12/29/1899 is ?.25, not ? + 0.25, which would be ?.75. There are no TDateTime values between ? and 0.程序:
========================procedure TForm1.Button2Click(Sender: TObject);
var
dat1,dat2:TDateTime;
dif:Double;days,hours,mins,secs:Integer;
begin
dat1:=EncodeDate(2000,10,1);
dat2:=Now; dif:=dat2-dat1; days:=Round(Int(dif)); dif:=dif-days;
secs:=round(dif * 24*60*60); hours:=secs div 3600;
mins:=(secs - hours*3600) div 60; showmessage(Format('%d ,%d, %d',[days,hours,mins]));
end;
要计算两个时间的间隔,TDateTime相减即可;
单独取出日月年,可用: yearof(),monthof(),dayof().
unit DateUtils;
[Delphi] procedure DecodeTime(const DateTime: TDateTime; var Hour: Word; var Min: Word; var Sec: Word; var MSec: Word);
T(小时)=cast((GetDate()-T1) as float)*24
T(分钟)=cast((GetDate()-T1) as float)*24*60
T(秒钟)=cast((GetDate()-T1) as float)*24*60*60
y,m,d:integer;
hh,mm,ss:integer;
begin
DecodeDate(now,y,m,d); --取出 年,月,日
decodeTime(now,hh,mm,ss); --取出 时,分,秒end;