delphi以特定格式获取小时和分钟的差异

delphi以特定格式获取小时和分钟的差异

本文介绍了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]);

这篇关于delphi以特定格式获取小时和分钟的差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

06-23 18:36