DateTimeInfo (C++)

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

TDateTime DateTime1, DateTime2;

void __fastcall TMainForm::btGetDateTime1Click(TObject *Sender)
{
  /* 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)))
    /* 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))
      edDateTime1->Text = DateTimeToStr(DateTime1);
    else;
  else
    ShowMessage("The date is not valid!");
}

void __fastcall TMainForm::btGetDateTime2Click(TObject *Sender)
{
  /* 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)) &&
      IsValidTime(StrToInt(edHour->Text),StrToInt(edMinute->Text), StrToInt(edSecond->Text),
      StrToInt(edMilliSecond->Text)))
    /* 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))
      edDateTime2->Text = DateTimeToStr(DateTime2);
    else;
  else
    ShowMessage("The date is not valid!");
}

void __fastcall TMainForm::btCompareClick(TObject *Sender)
{
  /* Compare the two DateTime variables */
  Memo1->Lines->Add(IntToStr(CompareDateTime(DateTime1,DateTime2)));
  if (SameDateTime(DateTime1, DateTime2))
    Memo1->Lines->Add("Is the same date and time");
  else if (SameDate(DateTime1,DateTime2))
    Memo1->Lines->Add("Is the same date");
  else if (SameTime(DateTime1,DateTime2))
    Memo1->Lines->Add("Is the same time");
}

Uses