RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
SysUtils.StrToDate Function

Converts a string to a TDateTime value.

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

SysUtils

Call StrToDate to parse a string that specifies a date. If S does not contain a valid date, StrToDate raises an EConvertError exception.  

S must consist of two or three numbers, separated by the character defined by the DateSeparator global variable or its TFormatSettings equivalent. The order for month, day, and year is determined by the ShortDateFormat global variable or its TFormatSettings equivalent--possible combinations are m/d/y, d/m/y, and y/m/d.  

If S contains only two numbers, it is interpreted as a date (m/d or d/m) in the current year. 

Year values between 0 and 99 are converted using the TwoDigitYearCenturyWindow. This value is stored either in a global variable (first form) or as a field in the FormatSettings parameter (second form) See "Currency and Date-Time Formatting Variables" for more information.  

The first form of StrToDate is not thread-safe, because it uses localization information contained in global variables. The second form of StrToDate, which is thread-safe, refers to localization information contained in the FormatSettings parameter. Before calling the thread-safe form of StrToDate, 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 date in the edit box in the format
associated with the current locale (for example MM/DD/YY
format in the US), the string entered is converted to a
TDateTime value. This value is used to indicate the day of
the week the date represents.
*/
void __fastcall TForm1::Button1Click(TObject *Sender)
{
  char days[7][10] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday" };
  TDateTime dtDate = StrToDate(Edit1->Text);
  ShowMessage(Edit1->Text + AnsiString(" is a ") + days[dtDate.DayOfWeek() - 1]);
}

 

Delphi Examples: 

{
This example uses an edit box and a button on a form. When
the user enters a date in the edit box in the format
associated with the current locale (for example MM/DD/YY
format in the US), the string entered is converted to a
TDateTime value. This value is used to indicate the day of
the week the date represents.
} 
procedure TForm1.Button1Click(Sender: TObject);
var
  ADate: TDateTime;
  days: array[1..7] of string;
begin
  days[1] := 'Sunday';
  days[2] := 'Monday';
  days[3] := 'Tuesday';
  days[4] := 'Wednesday';
  days[5] := 'Thursday';
  days[6] := 'Friday';
  days[7] := 'Saturday';
  ADate := StrToDate(Edit1.Text);
  ShowMessage(Edit1.Text + ' is a ' + days[SysUtils.DayOfWeek(ADate)]);
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!