DateTimeGen (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

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

Code

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

procedure TMainForm.btCompareClick(Sender: TObject);
begin
  { Compare the two dates }
  Memo2.Lines.Clear;
  if WithinPastDays(DateTime1, DateTime2, 10) then
    Memo2.Lines.Add('The difference between the two dates is less than 10 days');
  if WithinPastWeeks(DateTime1, DateTime2, 10) then
    Memo2.Lines.Add('The difference between the two dates is less than 10 weeks');
  if WithinPastMonths(DateTime1, DateTime2, 10) then
    Memo2.Lines.Add('The difference between the two dates is less than 10 months');
  if WithinPastYears(DateTime1, DateTime2, 10) then
    Memo2.Lines.Add('The difference between the two dates is less than 10 years');
  if WithinPastHours(DateTime1, DateTime2, 10) then
    Memo2.Lines.Add('The difference between the two dates is less than 10 hours');
  if WithinPastMinutes(DateTime1, DateTime2, 10) then
    Memo2.Lines.Add('The difference between the two dates is less than 10 minutes');
  if WithinPastSeconds(DateTime1, DateTime2, 10) then
    Memo2.Lines.Add('The difference between the two dates is less than 10 seconds');
  if WithinPastMilliSeconds(DateTime1, DateTime2, 10) then
    Memo2.Lines.Add('The difference between the two dates is less than 10 milliseconds');
end;

procedure TMainForm.btGetDateClick(Sender: TObject);
begin
  { 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);
end;

procedure TMainForm.DateTimePicker1Change(Sender: TObject);
begin
  { 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) then
     Memo1.Lines.Add('The selected date is today.');
  if IsSameDay(DateTime2, Yesterday) then
     Memo1.Lines.Add('The selected day is yesterday');
  if IsInLeapYear(DateTime2) then
    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)));
end;

Uses