Back to Devexpress

SmartTextToDate(string,TDateTime) Method

vcl-cxdateutils-dot-smarttexttodate-x28-3531e441-x29.md

latest5.8 KB
Original Source

SmartTextToDate(string,TDateTime) Method

Converts a string to the TDateTime type format.

Declaration

delphi
function SmartTextToDate(const AText: string; var ADate: TDateTime): Boolean;

Parameters

NameType
ATextstring
ADateTDateTime

Returns

Type
Boolean

Remarks

Use the SmartTextToDate function to convert a string to the TDateTime type format. If conversion succeeds, the function returns True ; otherwise, False. The strings that the SmartTextToDate function recognizes (also called smart input constants) are declared as resource strings in the cxLibraryStrs unit. For instance, for the ‘today’ string, the SmartTextToDate function retrieves the current date.

Built-in smart input constants are stored in the scxDateEditSmartInput array. Each array element corresponds to a TcxDateEditSmartInput enumeration value, which you can use as an index with the scxDateEditSmartInput array in order to obtain the respective smart input constant.

The following table lists all the built-in smart input constants.

Smart Input ConstantResource String IdentifierTcxDateEditSmartInput EquivalentDescription
‘today’cxSDateTodaydeiTodayRepresents the current date.
‘yesterday’cxSDateYesterdaydeiYesterdayRepresents yesterday’s date.
‘tomorrow’cxSDateTomorrowdeiTomorrowRepresents tomorrow’s date.
‘Sunday’cxSDateSundaydeiSundayRepresents the next Sunday date. *
‘Monday’cxSDateMondaydeiMondayRepresents the next Monday date. *
‘Tuesday’cxSDateTuesdaydeiTuesdayRepresents the next Tuesday date. *
‘Wednesday’cxSDateWednesdaydeiWednesdayRepresents the next Wednesday date. *
‘Thursday’cxSDateThursdaydeiThursdayRepresents the next Thursday date. *
‘Friday’cxSDateFridaydeiFridayRepresents the next Friday’s date. *
‘Saturday’cxSDateSaturdaydeiSaturdayRepresents the next Saturday’s date. *
‘first’cxSDateFirstdeiFirstRepresents the first day of the current week.
‘second’cxSDateSeconddeiSecondRepresents the second day of the current week.
‘third’cxSDateThirddeiThirdRepresents the third day of the current week.
‘fourth’cxSDateFourthdeiFourthRepresents the fourth day of the current week.
‘fifth’cxSDateFifthdeiFifthRepresents the fifth day of the current week.
‘sixth’cxSDateSixthdeiSixthRepresents the sixth day of the current week.
‘seventh’cxSDateSeventhdeiSeventhRepresents the seventh day of the current week.
‘bom’cxSDateBOMdeiBOMRepresents the first day of the current month.
‘eom’cxSDateEOMdeiEOMRepresents the last day of the current month.
‘now’cxSDateNowdeiNowRepresents the current date including the current time.

Depending on your language you may want to use another strings to be recognized by the function. For this purpose, you can use the global cxSetResourceString procedure which replaces the resource string for the given identifier. A date editor gets a string, when it needs to display it, via the cxGetResourceString function. The function retrieves a resource string for the given identifier taking into account whether the string was substituted by cxSetResourceString.

A date edit control allows a user to type specific strings instead of date values. These strings are interpreted then as corresponding dates. A date editor uses the SmartTextToDate function to convert the strings. The next code shows how to substitute the ‘today’ and ‘tomorrow’ strings acceptable by a date edit control for the German equivalents:

delphi
cxSetResourceString(@cxSDateToday, "Heute");
  cxSetResourceString(@cxSDateTomorrow, "Morgen");
  cxFormatController.GetFormats;
  cxFormatController.NotifyListeners;
cpp
cxSetResourceString(&_cxSDateToday, "Heute");
  cxSetResourceString(&_cxSDateTomorrow, "Morgen");
  cxFormatController -> GetFormats();
  cxFormatController -> NotifyListeners();

Now a date edit recognizes ‘Heute’ and ‘Morgen’ strings as today and tomorrow respectively. Calling to the SmartTextToDate function is performed by a date editor internally. After setting resource strings, you need to rebuild masks used by date editors. To do so, just call the GetFormats and NotifyListeners functions of the global format controller object.

You can implement your own parser function (SmartTextToDateFunc constant) and use it to parse input via the SmartTextToDate function.

See Also

TcxDBDateEdit

TcxDateEdit

Using the SmartTextToDate Function

cxDateUtils Unit