DateUtils (C++)

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

__fastcall TMainForm::TMainForm(TComponent* Owner)
	: TForm(Owner)
{
  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());
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::btGetInfoClick(TObject *Sender)
{
  /* 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)));
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::btGetFirstDateClick(TObject *Sender)
{
  /* Write the selected date in the first edit-box */
  edFirstDate->Text = DateToStr(DateOf(DateTimePicker1->DateTime));
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::btGetSecondDateClick(TObject *Sender)
{
  /* Write the selected date in the second edit-box */
  edSecondDate->Text = DateToStr(DateOf(DateTimePicker1->DateTime));
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::btCompareDatesClick(TObject *Sender)
{
  Integer CompResult;
  mmInfo->Lines->Clear();
  /* Compares the two dates */
  CompResult = 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 (CompResult == LessThanValue)
  	mmInfo->Lines->Add("The first date is before the second date.");
  if (CompResult == EqualsValue)
	mmInfo->Lines->Add("The first date is the same as the second date.");
  if (CompResult == GreaterThanValue)
	mmInfo->Lines->Add("The first date is after the second date.");
}

Uses