StrToDateTime

Convert a date/time string to a TDateTime value.

Declaration

Source position: datih.inc line 155

  function StrToDateTime(const S: AnsiString) : TDateTime;
  function StrToDateTime(const s: ShortString; 
                        const FormatSettings: TFormatSettings) : TDateTime;
  function StrToDateTime(const s: AnsiString; 
                        const FormatSettings: TFormatSettings) : TDateTime;

Description

StrToDateTime converts the string S to a TDateTime date and time value. The date and time parts must be separated by a space.

For the date part, the same restrictions apply as for the StrToDate function: The Date must consist of 1 to three numbers, separated by the DateSeparator character. If two numbers are given, they are supposed to form the day and month of the current year. If only one number is given, it is supposed to represent the day of the current month. (This is not supported in Delphi)

The order of the 3 numbers (y/m/d, m/d/y, d/m/y) is determined from the ShortDateFormat variable.

Errors

On error (e.g. an invalid date or invalid character), an EConvertError exception is raised.

See also

Name Description
DateTimeToStr Converts a TDateTime value to a string using a predefined format.
EConvertError Conversion error.
StrToDate Convert a date string to a TDateTime value.
StrToTime Convert a time string to a TDateTime value.

Example

Program Example20;
{ This program demonstrates the StrToDateTime function }
Uses sysutils;
Procedure TestStr (S : String);
begin
  Writeln (S,' : ',DateTimeToStr(StrToDateTime(S)));
end;
Begin
  Writeln ('ShortDateFormat ',ShortDateFormat);
  TestStr(DateTimeToStr(Now));
  TestStr('05-05-1999 15:50');
  TestStr('5-5 13:30');
  TestStr('5 1:30PM');
End.