DTValid (Delphi)

From RAD Studio Code Examples
Jump to: navigation, search

Description

 This example demonstrates the use of the decoding and recoding routines
 used for TDateTime variables.

Code

var
  DateTime: TDateTime;
  month, year, week, nthday, dayy, dayw, weeky : Word;

procedure TMainForm.btDecodeClick(Sender: TObject);
begin
  { Decode the date }
  if rgDecode.ItemIndex = 0 then
    begin
    DecodeDateDay(DateTime, year, dayy);
    edDayYear.Text := IntToStr(dayy);
    edYear.Text := IntToStr(year);
    end;
  if rgDecode.ItemIndex = 1 then
    begin
    DecodeDateWeek(DateTime,year,weeky,dayw);
    edYear.Text := IntToStr(year);
    edWeekYear.Text := IntToStr(weeky);
    edDayWeek.Text := IntToStr(dayw);
    end;
  if rgDecode.ItemIndex = 2 then
    begin
    DecodeDateMonthWeek(DateTime,year,month,week,dayw);
    edYear.Text := IntToStr(year);
    edMonth.Text := IntToStr(month);
    edWeek.Text := IntToStr(week);
    edDayWeek.Text := IntToStr(dayw);
    end;
  if rgDecode.ItemIndex = 3 then
    begin
    DecodeDayOfWeekInMonth(DateTime,year,month,nthday,dayw);
    edYear.Text := IntToStr(year);
    edMonth.Text := IntToStr(month);
    edDayWM.Text := IntToStr(nthday);
    edDayWeek.Text := IntToStr(dayw);
    end;
end;

procedure TMainForm.btEncodeClick(Sender: TObject);
begin
  { Encode the date }
  if rgEncode.ItemIndex = 0 then
    begin
    dayy := StrToInt(edDayYear.Text);
    year := StrToInt(edYear.Text);
    if IsValidDateDay(year,dayy) then begin
        DateTime := EncodeDateDay(year,dayy);
        edDate.Text := DateTimeToStr(DateTime);
      end else
      ShowMessage('Not a valid date');
    end;
  if rgEncode.ItemIndex = 1 then
    begin
    dayw := StrToInt(edDayWeek.Text);
    year := StrToInt(edYear.Text);
    weeky := StrToInt(edWeekYear.Text);
    if IsValidDateWeek(year,weeky,dayw) then begin
        DateTime := EncodeDateWeek(year,weeky,dayw);
        edDate.Text := DateTimeToStr(DateTime);
      end else
      ShowMessage('Not a valid date');
    end;
  if rgEncode.ItemIndex = 2 then
    begin
    dayw := StrToInt(edDayWeek.Text);
    year := StrToInt(edYear.Text);
    month := StrToInt(edMonth.Text);
    week := StrToInt(edWeek.Text);
    if IsValidDateMonthWeek(year, month, week, dayw) then begin
      DateTime := EncodeDateMonthWeek(year,month,week,dayw);
      edDate.Text := DateTimeToStr(DateTime);
    end else
      ShowMessage('Not a valid date');
    end;
  if rgEncode.ItemIndex = 3 then
    begin
    dayw := StrToInt(edDayWeek.Text);
    year := StrToInt(edYear.Text);
    month := StrToInt(edMonth.Text);
    nthday := StrToInt(edDayWM.Text);
    DateTime := EncodeDayOfWeekInMonth(year,month,nthday,dayw);
    edDate.Text := DateTimeToStr(DateTime);
    end;
end;

procedure TMainForm.FormCreate(Sender: TObject);
begin
  { Initialize the variable with the current date }
  DateTime := Date;
  edDate.Text := DateToStr(DateTime);
end;

Uses