RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
SysUtils.DateToStr Function

Converts a TDateTime value to a string.

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

Use DateToStr to obtain a string representation of a date value that can be used for display purposes. 

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

C++ Examples: 

 

/*
This example uses a label and a button on a form. When the
user clicks the button, the current date displays in the
caption of the label. Because some of the date variables are
assigned new values, the format of the date in the label
changes. For example, if the date is 9/15/94, the date
displays as 9-15-1994.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  DateSeparator = '-';
  ShortDateFormat = "m/d/yyyy";
  Label1->Caption = DateToStr(Date());
}
/*
This example uses a label and a button on a form. When the
user clicks the button, the current date is displayed in the
caption of the label:
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  Label1->Caption = DateToStr(Date());
}
/*
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 = EncodeDate((Word)StrToInt(Edit1->Text), (Word)StrToInt(Edit2->Text), (Word)StrToInt(Edit3->Text));
    Label1->Caption = DateToStr(dt);
  }
  catch(...)
  {
    ShowMessage("Invalid day, month, or year.");
  }
}

 

Delphi Examples: 

{
This example uses a label and a button on a form. When the 
user clicks the button, the current date displays in the 
caption of the label. Because some of the date variables are 
assigned new values, the format of the date in the label 
changes. For example, if the date is 9/15/94, the date 
displays as 9-15-1994.
} 
procedure TForm1.Button1Click(Sender: TObject);
begin
  DateSeparator := '-';
  ShortDateFormat := 'm/d/yyyy';
  Label1.Caption := DateToStr(Date);
end;
{
This example uses a label and a button on a form. When the
user clicks the button, the current date is displayed in the
caption of the label:
} 
procedure TForm1.Button1Click(Sender: TObject);
begin
  Label1.Caption := 'Today is  ' + SysUtils.DateToStr(Date);
end;
{
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.EncodeDate(StrToInt(Edit1.Text), StrToInt(Edit2.Text), StrToInt(Edit3.Text));
  Label1.Caption := DateToStr(MyDate);
end;

 

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