Converts a time string into a TDateTime value SysUtils unit
1  function StrToTime ( const Time : string ) : TDateTime;
2  function StrToTime ( const Time : string; const FormatSettings : TFormatSettings ) : TDateTime;
The StrToTime function attempts to convert a time as a string Date into a TDateTime value.
The time string 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
hour:minute:second.milli-seconds, where :
The hour  must be 0..23
The minute  must be 0..59 (optional)
The second  must be 0..59 (optional)
The milli-secs  must be 0..999 (optional)

You may use the current AM and PM value (as defined by TimeAMString, TimePMString), or the literals 'AM', 'am', 'PM' and 'pm' before or after the time.
The date is set to 30 dec 1899, one day short of the 19th century.
Any errors in the 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.
Warning : the date value is set to 1 day short of the end of the 19th century. Exactly why is unclear.
Related commands
LongTimeFormat Long version of the time to string format
StrToDate Converts a date string into a TDateTime value
StrToDateTime Converts a date+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
TimeToStr Converts a TDateTime time value to a string
 Author links

 Buy Website Traffic at

 Buy Proxies at
 Download this web site as a Windows program.

Example code : Showing with and without minutes and/or seconds
// 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;
  SysUtils,   // Unit containing the StrToTime command
  Forms, Dialogs;
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
Form1: TForm1;
{$R *.dfm} // Include form definitions
procedure TForm1.FormCreate(Sender: TObject);

  myTime : TDateTime;

  myTime := StrToTime('3PM');
  ShowMessage('3PM      = '+TimeToStr(mytime));
  myTime := StrToTime('15');
  ShowMessage('15       = '+TimeToStr(mytime));
  myTime := StrToTime('15:03');
  ShowMessage('15:03    = '+TimeToStr(mytime));
  myTime := StrToTime('15:03:45');
  ShowMessage('15:03:45 = '+TimeToStr(mytime));
Hide full unit code
   3PM      = 15:00:00
   15       = 15:00:00
   15:03    = 15:03:00
   15:03:45 = 15:03:45
Delphi Programming Neil Moffatt 2002 - 2018. All rights reserved.  |  Home Page