DelphiBasics
StrToDateTime
Function
Converts a date+time string into a TDateTime value SysUtils unit
1 function StrToDateTime(const DateTime string):TDateTime;
2 function StrToDateTime (const DateTime string; const FormatSettings TFormatSettings:TDateTime;
Description
The StrToDateTime function attempts to convert a date plus time as a string DateTime into a TDateTime value.
 
The first, date part of the string must adhere to the format of the ShortDateFormat value, and use the DateSeparator character to separate the day, month and year values.
 
The second, time part, separated by a blank from the date must adhere to the format of the LongTimeFormat value, and use the TimeSeparator character to separate the hour, minute and second values.
 
The default format in the UK is day/month/year hour:minute:second.msec, where :
 
The day  must be 1..31 (depending on month/year)
The month  must be 1..12
The year  must be 0..9999 (optional)
The hour  must be 0..23
The minute  must be 0..59 (optional)
The second  must be 0..59 (optional)
The milli-sec  must be 0..999 (optional)

 
Omitting the year results in the use of the current year.
 
Note that year 0015, for example, must be given with the century digits; 15 on its own would be seen as a recent year.
 
If the year is 2 digits, the century is determined by the TwoDigitYearCenturyWindow value.
 
Any errors in the date/time string will yield EConvertError.
 
Version 2 of this function is for use within threads. You furnish the FormatSettings record before invoking the call. It takes a local copy of global formatting variables that make the routine thread safe.
Related commands
DateSeparatorThe character used to separate display date fields
DateTimeToStrConverts TDateTime date and time values to a string
LongTimeFormatLong version of the time to string format
ShortDateFormatCompact version of the date to string format
StrToDateConverts a date string into a TDateTime value
StrToTimeConverts a time string into a TDateTime value
TimeAMStringDetermines AM value in DateTimeToString procedure
TimePMStringDetermines PM value in DateTimeToString procedure
TimeSeparatorThe character used to separate display time fields
 Download this web site as a Windows program.




 
Example code : Showing 2 and 4 digit year date string conversions
var
  myDateTime : TDateTime;

begin
  myDateTime := StrToDateTime('23/02/75   12');
  ShowMessage('23/02/75   12      = '+DateTimeToStr(myDateTime));

  myDateTime := StrToDateTime('23/02/2075 12:34:56');
  ShowMessage('23/02/2075 12:34:56 = '+DateTimeToStr(myDateTime));
end;
Show full unit code
  23/02/75 12 = 23/02/1975 12:00:00
  23/02/2075 12:34:56 = 23/02/2075 12:34:56
 
Delphi Programming © Neil Moffatt . All rights reserved.  |  Home Page