RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
SysUtils.StrToTime Function

Converts a string to a TDateTime value.

Pascal
function StrToTime(const S: string): TDateTime; overload;
function StrToTime(const S: string; const FormatSettings: TFormatSettings): TDateTime; overload;
C++
TDateTime StrToTime(const AnsiString S);
TDateTime StrToTime(const AnsiString S, const TFormatSettings FormatSettings);

Call StrToTime to parse a string that specifies a time value. If S does not contain a valid time, StrToTime raises an EConvertError exception.  

The S parameter must consist of two or three numbers, separated by the character defined by the TimeSeparator global variable or its TFormatSettings equivalent, optionally followed by an AM or PM indicator. The numbers represent hour, minute, and (optionally) second, in that order. If the time is followed by AM or PM, it is assumed to be in 12-hour clock format. If no AM or PM indicator is included, the time is assumed to be in 24-hour clock format.  

The first form of StrToTime is not thread-safe, because it uses localization information contained in global variables. The second form of StrToTime, which is thread-safe, refers to localization information contained in the FormatSettings parameter. Before calling the thread-safe form of StrToTime, you must populate FormatSettings with localization information. To populate FormatSettings with a set of default locale values, call GetLocaleFormatSettings.  

C++ Examples: 

 

/*
This example uses an edit box and a button on a form. When
the user enters a time in the edit box in the HH:MM:SS
format, the string entered is converted to a TDateTime value.
A message then appears that says "Good Morning" or "Good
Afternoon", depending on whether the time entered was in the
morning or afternoon.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TDateTime dtTime = StrToTime(Edit1->Text);
  if (dtTime < (TDateTime) 0.50 )
    ShowMessage("Good Morning");
  else
    ShowMessage("Good Afternoon");
}

 

Delphi Examples: 

{
This example uses an edit box and a button on a form. When
the user enters a time in the edit box in the HH:MM:SS
format, the string entered is converted to a TDateTime value.
A message then appears that says "Good Morning" or "Good
Afternoon", depending on whether the time entered was in the
morning or afternoon.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  ATime: TDateTime;
begin
  ATime := StrToTime(Edit1.Text);
  if ATime < 0.50 then
    ShowMessage('Good Morning')
  else
    ShowMessage('Good Afternoon');
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!