DateUtils (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example demonstrates the use of some routines included in the DateUtils unit. The form contains a TDateTimePicker and several TEdit boxes, used for rereading and writing elements of a TDateTime object.

Code

procedure TMainForm.btCompareDatesClick(Sender: TObject);
var
  Result: Integer;
begin
  mmInfo.Lines.Clear;
  { Compares the two dates }
  Result := CompareDate(StrToDate(edFirstDate.Text), StrToDate(edSecondDate.Text));
  { Write the date in the memo }
  mmInfo.Lines.Add('First date: ' + edFirstDate.Text);
  mmInfo.Lines.Add('Second date: ' + edSecondDate.Text);
  { Write the result of the comparison }
  if Result = LessThanValue then
    mmInfo.Lines.Add('The first date is before the second date.');
  if Result = EqualsValue then
    mmInfo.Lines.Add('The first date is the same as the second date.');
  if Result = GreaterThanValue then
    mmInfo.Lines.Add('The first date is after the second date.');
end;

procedure TMainForm.btGetFirstDateClick(Sender: TObject);
begin
  { Write the selected date in the first edit-box }
  edFirstDate.Text := DateToStr(DateOf(DateTimePicker1.DateTime));
end;

procedure TMainForm.btGetInfoClick(Sender: TObject);
begin
  { Get the day from the selected date }
  edDay.Text := IntToStr(DayOf(DateTimePicker1.Date));
  { Get the moonth from the selected date }
  edMonth.Text := IntToStr(MonthOf(DateTimePicker1.Date));
  { Get the year from the selected date }
  edYear.Text := IntToStr(YearOf(DateTimePicker1.Date));
  { Get the hour from the selected date }
  edHour.Text := IntToStr(HourOf(TimeOf(DateTimePicker1.Time)));
  { Get the minute from the selected date }
  edMinute.Text := IntToStr(MinuteOf(TimeOf(DateTimePicker1.Time)));
  { Get the second from the selected date }
  edSecond.Text := IntToStr(SecondOf(TimeOf(DateTimePicker1.Time)));
  { Get the millisecond from the selected date }
  edMilliSecond.Text := IntToStr(MilliSecondOf(TimeOf(DateTimePicker1.Time)));
end;

procedure TMainForm.btGetSecondDateClick(Sender: TObject);
begin
  { Write the selected date in the second edit-box }
  edSecondDate.Text := DateToStr(DateOf(DateTimePicker1.DateTime));
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  DateTimePicker1.Format := 'dd/MM/yyyy       hh:mm';
  { Get the current date and time }
  DateTimePicker1.Date := Date;
  DateTimePicker1.Time := Time;
  { Write the current date in the first edit-box }
  edFirstDate.Text := DateToStr(Today);
  { Write the previous date in the second edit-box }
  edSecondDate.Text := DateToStr(Yesterday);
end;

Uses