RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
SysUtils.TimeToStr Function

Returns a string that represents a TDateTime value.

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

TimeToStr converts the Time parameter, a TDateTime value, to a string. The conversion uses the format specified by the LongTimeFormat global variable or its TFormatSettings equivalent. Change the format of the string by changing the values of some of the date and time variables. 

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

C++ Examples: 

 

/*
This example uses three edit controls, a label, and a button
on a form. When the button is clicked, the day, month, and
year specified in the edit controls is stored in a TDateTime
object. Then the TDateTime object is converted to an
AnsiString that is displayed in the label.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  TDateTime dt;
  try
  {
    dt = EncodeTime((Word)StrToInt(Edit1->Text), (Word)StrToInt(Edit2->Text), (Word)StrToInt(Edit3->Text), (Word)StrToInt(Edit4->Text));
    Label1->Caption = TimeToStr(dt);
  }
  catch(...)
  {
    ShowMessage("Invalid hour, minute, second, or millisecond.");
  }
}
/*
This procedure displays the current time in the form's
caption every time the Timer's OnClick event fires, based upon
the Interval Property (default: 1000 = 1 sec)
*/
void __fastcall TForm1::Timer1Timer(TObject *Sender)
{
  TDateTime DateTime = Time();  // store the current date and time
  AnsiString str = TimeToStr(DateTime); // convert the time to a string
  Caption = str;  // display the time on the form's caption
  // Note This could have been done with the following line of code:
  //  Caption = TimeToStr(Time());
}

 

Delphi Examples: 

{
This example uses three edit controls, a label, and a button
on a form. When the button is clicked, the day, month, and
year specified in the edit controls is stored in a TDateTime
object. Then the TDateTime object is converted to an
AnsiString that is displayed in the label.
}
procedure TForm1.Button1Click(Sender: TObject);
var
  MyDate: TDateTime;
begin
  MyDate := SysUtils.EncodeTime(StrToInt(Edit1.Text), StrToInt(Edit2.Text), StrToInt(Edit3.Text), StrToInt(Edit4.Text));
  Label1.Caption := TimeToStr(MyDate);
end;
{
This procedure displays the current time in the form's
caption every time the Timer's OnClick event fires, based upon
the Interval Property (default: 1000 = 1 sec)
}
procedure TForm1.Timer1Timer(Sender: TObject);
var
  DateTime : TDateTime;
  str : string;
begin
  DateTime := Time;  // store the current date and time
  str := TimeToStr(DateTime); // convert the time into a string
  Caption := str;  // display the time on the form's caption
  { Note This could have been done with the following line of code:
    Caption := TimeToStr(Time); }
end;

 

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