DTStartEnd (C++)

From Appmethod Code Examples
Jump to: navigation, search

Description

This example demonstrates the use of some routines in the DateUtils unit.

Code

TDateTime DateTime;

__fastcall TMainForm::TMainForm(TComponent* Owner)
	: TForm(Owner)
{
  DateTime = Date() + Time();
  /* Verify whether the time portion of the TDateTime variable occurs after noon. */
  if (IsPM(DateTime))
	ShowMessage("It is after-noon");
  else
	ShowMessage("It is morning");

  /* Write the start and the end of current date. */
  Edit1->Text = DateTimeToStr(DateTime);
  Memo1->Lines->Add("Start of the year: " + DateTimeToStr(StartOfTheYear(DateTime)));
  Memo1->Lines->Add("Start of the month: " + DateTimeToStr(StartOfTheMonth(DateTime)));
  Memo1->Lines->Add("Start of the week: " + DateTimeToStr(StartOfTheWeek(DateTime)));
  Memo1->Lines->Add("Start of the day: " + DateTimeToStr(StartOfTheDay(DateTime)));
  Memo1->Lines->Add("Start of a year: " + DateTimeToStr(StartOfAYear(YearOf(DateTime))));
  Memo1->Lines->Add("Start of a month: " + DateTimeToStr(StartOfAMonth(YearOf(DateTime),MonthOf(DateTime))));
  Memo1->Lines->Add("Start of a week: " + DateTimeToStr(StartOfAWeek(YearOf(DateTime),WeekOf(DateTime))));
  Memo1->Lines->Add("Start of a day: " + DateTimeToStr(StartOfADay(YearOf(DateTime),DayOf(DateTime))));

  Memo1->Lines->Add("End of the year: " + DateTimeToStr(EndOfTheYear(DateTime)));
  Memo1->Lines->Add("End of the month: " + DateTimeToStr(EndOfTheMonth(DateTime)));
  Memo1->Lines->Add("End of the week: " + DateTimeToStr(EndOfTheWeek(DateTime)));
  Memo1->Lines->Add("End of the day: " + DateTimeToStr(EndOfTheDay(DateTime)));
  Memo1->Lines->Add("End of a year: " + DateTimeToStr(EndOfAYear(YearOf(DateTime))));
  Memo1->Lines->Add("End of a month: " + DateTimeToStr(EndOfAMonth(YearOf(DateTime),MonthOf(DateTime))));
  Memo1->Lines->Add("End of a week: " + DateTimeToStr(EndOfAWeek(YearOf(DateTime),WeekOf(DateTime))));
  Memo1->Lines->Add("End of a day: " + DateTimeToStr(EndOfADay(YearOf(DateTime),DayOf(DateTime))));
}

void __fastcall TMainForm::btProcessClick(TObject *Sender)
{
  Integer number;

  Memo1->Lines->Clear();
  number = StrToInt(edInc->Text);

  /* Increase a specific portion of the TDateTime variable. */
  if (RadioGroup1->ItemIndex == 0)
	DateTime = IncMilliSecond(DateTime,number);
  if (RadioGroup1->ItemIndex == 1)
	DateTime = IncSecond(DateTime,number);
  if (RadioGroup1->ItemIndex == 2)
	DateTime = IncMinute(DateTime,number);
  if (RadioGroup1->ItemIndex == 3)
	DateTime = IncHour(DateTime,number);
  if (RadioGroup1->ItemIndex == 4)
	DateTime = IncDay(DateTime,number);
  if (RadioGroup1->ItemIndex == 5)
	DateTime = IncMonth(DateTime,number);
  if (RadioGroup1->ItemIndex == 6)
    DateTime = IncYear(DateTime,number);
  Edit1->Text = DateTimeToStr(DateTime);
}

Uses