Skip to main content

SmartTextToDate(string,TDateTime) Method

Converts a string to the TDateTime type format.

Declaration

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

Parameters

Name Type
AText string
ADate TDateTime

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 Constant Resource String Identifier TcxDateEditSmartInput Equivalent Description
‘today’ cxSDateToday deiToday Represents the current date.
‘yesterday’ cxSDateYesterday deiYesterday Represents yesterday’s date.
‘tomorrow’ cxSDateTomorrow deiTomorrow Represents tomorrow’s date.
‘Sunday’ cxSDateSunday deiSunday Represents the next Sunday date. *
‘Monday’ cxSDateMonday deiMonday Represents the next Monday date. *
‘Tuesday’ cxSDateTuesday deiTuesday Represents the next Tuesday date. *
‘Wednesday’ cxSDateWednesday deiWednesday Represents the next Wednesday date. *
‘Thursday’ cxSDateThursday deiThursday Represents the next Thursday date. *
‘Friday’ cxSDateFriday deiFriday Represents the next Friday’s date. *
‘Saturday’ cxSDateSaturday deiSaturday Represents the next Saturday’s date. *
‘first’ cxSDateFirst deiFirst Represents the first day of the current week.
‘second’ cxSDateSecond deiSecond Represents the second day of the current week.
‘third’ cxSDateThird deiThird Represents the third day of the current week.
‘fourth’ cxSDateFourth deiFourth Represents the fourth day of the current week.
‘fifth’ cxSDateFifth deiFifth Represents the fifth day of the current week.
‘sixth’ cxSDateSixth deiSixth Represents the sixth day of the current week.
‘seventh’ cxSDateSeventh deiSeventh Represents the seventh day of the current week.
‘bom’ cxSDateBOM deiBOM Represents the first day of the current month.
‘eom’ cxSDateEOM deiEOM Represents the last day of the current month.
‘now’ cxSDateNow deiNow Represents 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:

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