delphi以特定格式获取小时和分钟的差异
问题描述:
正在使用dateutils并进行了一些实验.
Was playing with dateutils and did some experimenting.
procedure TForm1.Button1Click(Sender: TObject);
var
fromDate, toDate : TDateTime;
begin
fromDate := cxDateEdit1.Date ;
toDate := cxDateEdit2.Date ;
Label1.Caption := 'Hour difference '+IntToStr(HoursBetween(toDate, fromDate))+' hours';
Label2.Caption := 'Minute difference '+IntToStr(MinutesBetween(toDate, fromDate))+' minutes';
end;
如何获得类似hh/mm的标签标题的时差结果(示例01:05)?
How can I get a time difference result in a label caption like hh/mm (example 01:05) ???
答
SysUtils.FormatDateTime 具有许多有用的 TDateTime
来进行字符串转换:
SysUtils.FormatDateTime has many useful TDateTime
to string conversions:
Label3.Caption := 'Time difference [hh:mm] '+FormatDateTime('hh:nn',toDate-fromDate);
或者,使用 MinutesBetween
中的结果:
var
minutes: Integer;
...
minutes := MinutesBetween(toDate,FromDate);
Label3.Caption :=
'Time difference [hh:mm] '+Format('%.2d:%.2d',[minutes div 60,minutes mod 60]);