VariantsVarToDateTime (Object Pascal)

From Appmethod Code Examples
Jump to: navigation, search

Description

This example demonstrates the use of the VarToDateTime function in the Variants unit.

Code

var
  LDate: TDateTime;
  VDateString: Variant;
  LStr: String;

begin
  { Wait for user input. }
  Write('Write an input date/time (MM/DD/YYYY): '); Readln(LStr);

  { Assign the string to a Variant. }
  VDateString := LStr;

  try
    { Convert the preassigned Variant to TDateTime. }
    LDate := VarToDateTime(VDateString);

    { Write the value to the console. }
    Writeln(DateTimeToStr(LDate));
    Readln;
  except
    on EVariantTypeCastError do
    begin
      Writeln('Invalid date/time string supplied!');
      Exit;
    end;
  end;
end.

Uses