Converting a string to TDateTime based on an arbitrary format

Use VarToDateTime instead. It supports many more date formats in the string and converts them automatically.

var
  DateVal: TDateTime;
begin
  DateVal := VarToDateTime('23 Sep 2010');
  ShowMessage(DateToStr(DateVal));
end;

I see you’re using Delphi 5. Some versions of Delphi will need to add Variants to the uses clause; most later versions add it for you. I don’t remember which category Delphi 5 fell into.

Leave a Comment