将TDateTime值分解为小时、分钟、秒和毫秒,以及计算时间差

发布时间 2023-04-11 19:59:12作者: 六十五度

 

将时间日期分解

procedure TForm1.Button1Click(Sender: TObject);
var
  Present: TDateTime;
  Year, Month, Day, Hour, Min, Sec, MSec: Word;
 begin
  Present:= Now;
  SysUtils.DecodeDate(Present, Year, Month, Day);
  Label1.Caption := 'Today is Day ' + IntToStr(Day) + ' of Month '
    + IntToStr(Month) + ' of Year ' + IntToStr(Year);
  SysUtils.DecodeTime(Present, Hour, Min, Sec, MSec);
  Label2.Caption := 'The time is Minute ' + IntToStr(Min) + ' of Hour '
    + IntToStr(Hour);
end;

时间差

procedure TForm1.Button1Click(Sender: TObject);
var
  Year, Month, Day, Hour, Min, Sec, MSec: Word;
  Hour1, Min1, Sec1, MSec1: Word;
 begin
  DecodeTime(now, Hour, Min, Sec, MSec);
  DecodeTime(DateTimePicker1.Time,Hour1, Min1, Sec1, MSec1);
  Edit1.Text:=inttostr((hour*3600 + min*60 + sec)-(hour1*3600 + min1*60 + sec1)) ;
end;