DTValid (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example demonstrates the use of the decoding and recoding routines used for TDateTime variables.

 TDateTime DateTime;
 Word month, year, week, nthday, dayy, dayw, weeky;

Code

__fastcall TMainForm::TMainForm(TComponent* Owner)
	: TForm(Owner)
{
  /* Initialize the variable with the current date. */
  DateTime = Date();
  edDate->Text = DateToStr(DateTime);
}
//---------------------------------------------------------------------------
void __fastcall TMainForm::btDecodeClick(TObject *Sender)
{
  /* Decode the date. */
  if (rgDecode->ItemIndex == 0)
	{
	DecodeDateDay(DateTime, year, dayy);
	edDayYear->Text = IntToStr(dayy);
	edYear->Text = IntToStr(year);
	};
  if (rgDecode->ItemIndex == 1)
	{
	DecodeDateWeek(DateTime,year,weeky,dayw);
	edYear->Text = IntToStr(year);
	edWeekYear->Text = IntToStr(weeky);
	edDayWeek->Text = IntToStr(dayw);
	};
  if (rgDecode->ItemIndex == 2)
	{
	DecodeDateMonthWeek(DateTime,year,month,week,dayw);
	edYear->Text = IntToStr(year);
	edMonth->Text = IntToStr(month);
	edWeek->Text = IntToStr(week);
	edDayWeek->Text = IntToStr(dayw);
	};
  if (rgDecode->ItemIndex == 3)
	{
	DecodeDayOfWeekInMonth(DateTime,year,month,nthday,dayw);
	edYear->Text = IntToStr(year);
	edMonth->Text = IntToStr(month);
	edDayWM->Text = IntToStr(nthday);
	edDayWeek->Text = IntToStr(dayw);
	};
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::btEncodeClick(TObject *Sender)
{
  /* Encode the date. */
  if (rgEncode->ItemIndex == 0)
	{
	dayy = StrToInt(edDayYear->Text);
	year = StrToInt(edYear->Text);
	if (IsValidDateDay(year,dayy)) {
		DateTime = EncodeDateDay(year,dayy);
        edDate->Text = DateTimeToStr(DateTime);
      } else
      ShowMessage("Not a valid date");
	};
  if (rgEncode->ItemIndex == 1)
    {
    dayw = StrToInt(edDayWeek->Text);
    year = StrToInt(edYear->Text);
    weeky = StrToInt(edWeekYear->Text);
    if (IsValidDateWeek(year,weeky,dayw)) {
        DateTime = EncodeDateWeek(year,weeky,dayw);
        edDate->Text = DateTimeToStr(DateTime);
      } else
      ShowMessage("Not a valid date");
    };
  if (rgEncode->ItemIndex == 2)
    {
    dayw = StrToInt(edDayWeek->Text);
    year = StrToInt(edYear->Text);
    month = StrToInt(edMonth->Text);
    week = StrToInt(edWeek->Text);
	if (IsValidDateMonthWeek(year, month, week, dayw)) {
	  DateTime = EncodeDateMonthWeek(year,month,week,dayw);
      edDate->Text = DateTimeToStr(DateTime);
    } else
	  ShowMessage("Not a valid date");
    };
  if (rgEncode->ItemIndex == 3)
	{
	dayw = StrToInt(edDayWeek->Text);
    year = StrToInt(edYear->Text);
    month = StrToInt(edMonth->Text);
    nthday = StrToInt(edDayWM->Text);
    DateTime = EncodeDayOfWeekInMonth(year,month,nthday,dayw);
	edDate->Text = DateTimeToStr(DateTime);
	};
}

Uses