DateTimeInfo (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example uses several edit boxes for introducing date and time elements and cumulates them into TDateTime variables.

Code

var
  DateTime1, DateTime2: TDateTime;

procedure TMainForm.btCompareClick(Sender: TObject);
begin
  { Compare the two DateTime variables }
  Memo1.Lines.Add(IntToStr(CompareDateTime(DateTime1,DateTime2)));
  if SameDateTime(DateTime1, DateTime2) then
    Memo1.Lines.Add('Is the same date and time')
  else if SameDate(DateTime1,DateTime2) then
    Memo1.Lines.Add('Is the same date')
  else if SameTime(DateTime1,DateTime2) then
    Memo1.Lines.Add('Is the same time');
end;

procedure TMainForm.btGetDateTime1Click(Sender: TObject);
begin
  { Verify whether the numbers introduced by the user can form a valid DateTime variable }
  if IsValidDateTime(StrToInt(edYear.Text), StrToInt(edMonth.Text), StrToInt(edDay.Text),
                     StrToInt(edHour.Text), StrToInt(edMinute.Text),StrToInt(edSecond.Text),
                     StrToInt(edMilliSecond.Text)) then
    { Save the first date }
    if TryEncodeDateTime(StrToInt(edYear.Text), StrToInt(edMonth.Text), StrToInt(edDay.Text),
                         StrToInt(edHour.Text), StrToInt(edMinute.Text), StrToInt(edSecond.Text),
                         StrToInt(edMilliSecond.Text), DateTime1) then
      edDateTime1.Text := DateTimeToStr(DateTime1);
  else
    ShowMessage('The date is not valid!');
end;

procedure TMainForm.btGetDateTime2Click(Sender: TObject);
begin
  { Verify whether the numbers introduced by the user can form a valid DateTime variable }
  if IsValidDate(StrToInt(edYear.Text), StrToInt(edMonth.Text), StrToInt(edDay.Text)) and 
     IsValidTime(StrToInt(edHour.Text), StrToInt(edMinute.Text), StrToInt(edSecond.Text),
                 StrToInt(edMilliSecond.Text)) then
    { Save the second date }
    if TryEncodeDateTime(StrToInt(edYear.Text), StrToInt(edMonth.Text), StrToInt(edDay.Text),
                         StrToInt(edHour.Text), StrToInt(edMinute.Text), StrToInt(edSecond.Text),
                         StrToInt(edMilliSecond.Text),DateTime2) then
      edDateTime2.Text := DateTimeToStr(DateTime2);
  else
    ShowMessage('The date is not valid!');
end;

Uses