DateTimeCompare (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

The following example demonstrates the use of some date-time routines.

Code

TTime time1, time2;
TDate date1, date2;

__fastcall TMainForm::TMainForm(TComponent* Owner)
	: TForm(Owner)
{
  /* Insert the current time and date in the edit-boxes */
  time1 = Time();
  time2 = Time();
  date1 = Today();
  date2 = Tomorrow();
  edTime1->Text = TimeToStr(time1);
  edTime2->Text = TimeToStr(time2);
  edDate1->Text = DateToStr(date1);
  edDate2->Text = DateToStr(date2);
}

void __fastcall TMainForm::btCompareDateClick(TObject *Sender)
{
  /* Compare the two dates */
  Memo1->Lines->Clear();
  date1 = StrToDate(edDate1->Text);
  date2 = StrToDate(edDate2->Text);
  Memo1->Lines->Add(IntToStr(CompareDate(date1, date2)));
  Memo1->Lines->Add("Day span: " + FloatToStr(DaySpan(date1, date2)));
  Memo1->Lines->Add("Days between: " + FloatToStr(DaysBetween(date1, date2)));
  Memo1->Lines->Add("Week span: " + FloatToStr(WeekSpan(date1, date2)));
  Memo1->Lines->Add("Weeks between: "+ FloatToStr(WeeksBetween(date1, date2)));
  Memo1->Lines->Add("Month span: " + FloatToStr(MonthSpan(date1, date2)));
  Memo1->Lines->Add("Months between: "+ FloatToStr(MonthsBetween(date1, date2)));
  Memo1->Lines->Add("Year span: " + FloatToStr(YearSpan(date1, date2)));
  Memo1->Lines->Add("Years between: " + FloatToStr(YearsBetween(date1, date2)));
}

void __fastcall TMainForm::btCompareTimeClick(TObject *Sender)
{
  /* Compare the two times */
  Memo1->Lines->Clear();
  time1 = StrToTime(edTime1->Text);
  time2 = StrToTime(edTime2->Text);
  Memo1->Lines->Add(IntToStr(CompareTime(time1, time2)));
  Memo1->Lines->Add("Hour span: " + FloatToStr(HourSpan(time1, time2)));
  Memo1->Lines->Add("Hours between: " + FloatToStr(HoursBetween(time1, time2)));
  Memo1->Lines->Add("Minute span: " + FloatToStr(MinuteSpan(time1, time2)));
  Memo1->Lines->Add("Minutes between: " + FloatToStr(MinutesBetween(time1, time2)));
  Memo1->Lines->Add("Second span: " + FloatToStr(SecondSpan(time1, time2)));
  Memo1->Lines->Add("Seconds between: " + FloatToStr(SecondsBetween(time1, time2)));
  Memo1->Lines->Add("Millisecond span: " + FloatToStr(MilliSecondSpan(time1, time2)));
  Memo1->Lines->Add("Milliseconds between: " + FloatToStr(MilliSecondsBetween(time1, time2)));
}

void __fastcall TMainForm::btGetDateInfoClick(TObject *Sender)
{
  /* Write information about the date */
  Memo1->Lines->Clear();
  date1 = StrToDate(edDate1->Text);
  Memo1->Lines->Add("Day in week: " + IntToStr(DayOfTheWeek(date1)));
  Memo1->Lines->Add("Day in month: " + IntToStr(DayOfTheMonth(date1)));
  Memo1->Lines->Add("Day in year: " + IntToStr(DayOfTheYear(date1)));
  Memo1->Lines->Add("Week of the date: " + IntToStr(WeekOf(date1)));
  Memo1->Lines->Add("Week in month: " + IntToStr(WeekOfTheMonth(date1)));
  Memo1->Lines->Add("Week in year: " + IntToStr(WeekOfTheYear(date1)));
  Memo1->Lines->Add("Month in year: " + IntToStr(MonthOfTheYear(date1)));
}

void __fastcall TMainForm::btGetTimeInfoClick(TObject *Sender)
{
  /* Write information about the time */
  Memo1->Lines->Clear();
  time1 = StrToTime(edTime1->Text);
  Memo1->Lines->Add("Millisecond of the second: " +
	FloatToStr(MilliSecondOfTheSecond(time1)));
  Memo1->Lines->Add("Millisecond of the minute: " +
	FloatToStr(MilliSecondOfTheMinute(time1)));
  Memo1->Lines->Add("Millisecond of the hour: " +
	FloatToStr(MilliSecondOfTheHour(time1)));
  Memo1->Lines->Add("Millisecond of the day: " +
	FloatToStr(MilliSecondOfTheDay(time1)));
  Memo1->Lines->Add("Millisecond of the month: " +
	FloatToStr(MilliSecondOfTheMonth(time1)));
  Memo1->Lines->Add("Millisecond of the year: " +
	FloatToStr(MilliSecondOfTheYear(time1)));
  Memo1->Lines->Add("Second of the minute: " +
	FloatToStr(SecondOfTheMinute(time1)));
  Memo1->Lines->Add("Second of the hour: " +
	FloatToStr(SecondOfTheHour(time1)));
  Memo1->Lines->Add("Second of the day: " +
	FloatToStr(SecondOfTheDay(time1)));
  Memo1->Lines->Add("Second of the month: " +
	FloatToStr(SecondOfTheMonth(time1)));
  Memo1->Lines->Add("Second of the year: " +
	FloatToStr(SecondOfTheYear(time1)));
  Memo1->Lines->Add("Minute of the hour: " +
	FloatToStr(MinuteOfTheHour(time1)));
  Memo1->Lines->Add("Minute of the day: " +
	FloatToStr(MinuteOfTheDay(time1)));
  Memo1->Lines->Add("Minute of the month: " +
	FloatToStr(MinuteOfTheMonth(time1)));
  Memo1->Lines->Add("Minute of the Year: " +
	FloatToStr(MinuteOfTheYear(time1)));
  Memo1->Lines->Add("Hour of the day: " +
	FloatToStr(HourOfTheDay(time1)));
  Memo1->Lines->Add("Hour of the month: " +
	FloatToStr(HourOfTheMonth(time1)));
  Memo1->Lines->Add("Hour of the Year: " +
	FloatToStr(HourOfTheYear(time1)));
}

Uses