DelphiBasics
StrToTime
Function
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;
Description
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.
Notes
Warning : the date value is set to 1 day short of the end of the 19th century. Exactly why is unclear.
Related commands
LongTimeFormatLong version of the time to string format
StrToDateConverts a date string into a TDateTime value
StrToDateTimeConverts a date+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
TimeToStrConverts a TDateTime time value to a string
 Download this web site as a Windows program.




 
Example code : Showing with and without minutes and/or seconds
var
  myTime : TDateTime;

begin
  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));
end;
Show 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 . All rights reserved.  |  Home Page