DateTimeRecode (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

This example replaces specific parts of a TDateTime variable with values given by the user.

Code

var
  DateTime: TDateTime;

procedure TMainForm.btRecodeClick(Sender: TObject);
begin
  { Modify the TDateTime variable, according to the user's options. }
  if cbRecodeDay.Checked and cbRecodeMonth.Checked and cbRecodeYear.Checked and
    cbRecodeHour.Checked and cbRecodeMinute.Checked and
    cbRecodeSecond.Checked and cbRecodeMilliSecond.Checked
    then
  DateTime := RecodeDateTime(DateTime,
    StrToInt(edYear.Text),StrToInt(edMonth.Text),StrToInt(edDay.Text),
    StrToInt(edHour.Text),StrToInt(edMinute.Text),StrToInt(edSecond.Text),
    StrToInt(edMilliSecond.Text))
  else
  if cbRecodeDay.Checked and cbRecodeMonth.Checked and cbRecodeYear.Checked
    then
  DateTime := RecodeDate(DateTime,
    StrToInt(edYear.Text),StrToInt(edMonth.Text),StrToInt(edDay.Text))
  else
  if cbRecodeHour.Checked and cbRecodeMinute.Checked and
    cbRecodeSecond.Checked and cbRecodeMilliSecond.Checked
    then
  DateTime := RecodeTime(DateTime,StrToInt(edHour.Text),
    StrToInt(edMinute.Text),StrToInt(edSecond.Text),StrToInt(edMilliSecond.Text))
  else
  begin
  if cbRecodeDay.Checked then
    DateTime := RecodeDay(DateTime,StrToInt(edDay.Text));
  if cbRecodeMonth.Checked then
    DateTime := RecodeMonth(DateTime,StrToInt(edMonth.Text));
  if cbRecodeYear.Checked then
    DateTime := RecodeYear(DateTime,StrToInt(edYear.Text));
  if cbRecodeHour.Checked then
    DateTime := RecodeHour(DateTime,StrToInt(edHour.Text));
  if cbRecodeMinute.Checked then
    DateTime := RecodeMinute(DateTime,StrToInt(edMinute.Text));
  if cbRecodeSecond.Checked then
    DateTime := RecodeSecond(DateTime,StrToInt(edSecond.Text));
  if cbRecodeMilliSecond.Checked then
    DateTime := RecodeMilliSecond(DateTime,StrToInt(edMilliSecond.Text));
  end;
  edDate.Text := DateTimeToStr(DateTime);
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  { Get the current date and time. }
  DateTime := Date + Time;
  { Write the day of the week, date and time }
  if NthDayOfWeek(DateTime) = DayMonday then
    edDate.Text := 'Monday, ' + DateTimeToStr(DateTime);
  if NthDayOfWeek(DateTime) = DayTuesday then
    edDate.Text := 'Tuesday, ' + DateTimeToStr(DateTime);
  if NthDayOfWeek(DateTime) = DayWednesday then
    edDate.Text := 'Wednesday, ' + DateTimeToStr(DateTime);
  if NthDayOfWeek(DateTime) = DayTuesday then
    edDate.Text := 'Tuesday, ' + DateTimeToStr(DateTime);
  if NthDayOfWeek(DateTime) = DayFriday then
    edDate.Text := 'Friday, ' + DateTimeToStr(DateTime);
  if NthDayOfWeek(DateTime) = DaySaturday then
    edDate.Text := 'Saturday, ' + DateTimeToStr(DateTime);
  if NthDayOfWeek(DateTime) = DaySunday then
    edDate.Text := 'Sunday, ' + DateTimeToStr(DateTime);
end;

Uses

Personal tools
Assistance