DateTimeGen (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This examples demonstrates the use of DateUtils routines in comparing two DateTime variables.

TDateTime DateTime1, DateTime2; Word year, month, day, hour, minute, second, millisecond;

Code

void __fastcall TMainForm::DateTimePicker1Change(TObject *Sender)
{
  /* Get the second DateTime variable from the DateTimePicker */
  DateTime2 = DateTimePicker1->DateTime;
  DecodeDateTime(DateTime2,year,month,day,hour,minute,second,millisecond);
  Memo1->Lines->Clear();
  edDateTime2->Text = DateTimeToStr(DateTime2);
  if (IsToday(DateTime2))
	 Memo1->Lines->Add("The selected date is today->");
  if (IsSameDay(DateTime2, Yesterday()))
	 Memo1->Lines->Add("The selected day is yesterday");
  if (IsInLeapYear(DateTime2))
	Memo1->Lines->Add("The selected date is in a leap year");
  else
	Memo1->Lines->Add("The selected date is not in a leap year");
  Memo1->Lines->Add("Days in a month: "+IntToStr(DaysInAMonth(year,month)));
  Memo1->Lines->Add("Days in the current month: "+
	IntToStr(DaysInMonth(DateTime2)));
  Memo1->Lines->Add("Days in a year: "+IntToStr(DaysInYear(DateTime2)));
  Memo1->Lines->Add("Days in the current year: "+
	IntToStr(DaysInAYear(year)));
  Memo1->Lines->Add("Weeks in a year: "+IntToStr(WeeksInYear(DateTime2)));
  Memo1->Lines->Add("Weeks in the current year: "+
	IntToStr(WeeksInAYear(year)));
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::btGetDateClick(TObject *Sender)
{
  /* Get the first DateTime variable from the edit-boxes */
  year = StrToInt(edYear->Text);
  month = StrToInt(edMonth->Text);
  day = StrToInt(edDay->Text);
  hour = StrToInt(edHour->Text);
  minute = StrToInt(edMinute->Text);
  second = StrToInt(edSecond->Text);
  millisecond = StrToInt(edMilliSecond->Text);
  DateTime1 = EncodeDateTime(year,month,day,hour,minute,second,millisecond);
  edDateTime1->Text = DateTimeToStr(DateTime1);
}
//---------------------------------------------------------------------------

void __fastcall TMainForm::btCompareClick(TObject *Sender)
{
  /* Compare the two dates */
  Memo2->Lines->Clear();
  if (WithinPastDays(DateTime1, DateTime2, 10))
	Memo2->Lines->Add("The difference between the two dates is less than 10 days");
  if (WithinPastWeeks(DateTime1, DateTime2, 10))
	Memo2->Lines->Add("The difference between the two dates is less than 10 weeks");
  if (WithinPastMonths(DateTime1, DateTime2, 10))
	Memo2->Lines->Add("The difference between the two dates is less than 10 months");
  if (WithinPastYears(DateTime1, DateTime2, 10))
	Memo2->Lines->Add("The difference between the two dates is less than 10 years");
  if (WithinPastHours(DateTime1, DateTime2, 10))
	Memo2->Lines->Add("The difference between the two dates is less than 10 hours");
  if (WithinPastMinutes(DateTime1, DateTime2, 10))
	Memo2->Lines->Add("The difference between the two dates is less than 10 minutes");
  if (WithinPastSeconds(DateTime1, DateTime2, 10))
	Memo2->Lines->Add("The difference between the two dates is less than 10 seconds");
  if (WithinPastMilliSeconds(DateTime1, DateTime2, 10))
	Memo2->Lines->Add("The difference between the two dates is less than 10 milliseconds");
}

Uses