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
DateSeparator The character used to separate display date fields
DateTimeToStr Converts TDateTime date and time values to a string
LongTimeFormat Long version of the time to string format
ShortDateFormat Compact version of the date to string format
StrToDate Converts a date string into a TDateTime value
StrToTime Converts a time string into a TDateTime value
TimeAMString Determines AM value in DateTimeToString procedure
TimePMString Determines PM value in DateTimeToString procedure
TimeSeparator The 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
// Full Unit code.
// -----------------------------------------------------------
// You must store this code in a unit called Unit1 with a form
// called Form1 that has an OnCreate event called FormCreate.
 
unit Unit1;
 
interface
 
uses
  SysUtils,   // Unit containing the StrToDateTime command
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

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;
 
end.
Hide 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 2002 - 2013. All rights reserved.  |  Contact the author  |  Home Page