Xojo.Core.Date.ToText

From Xojo Documentation

Method

Xojo.Core.Date.ToText(loc As Xojo.Core.Locale = Nil, dateStyle As Xojo.Core.Date.FormatStyles = Xojo.Core.Date.FormatStyles.Medium, timeStyle As Xojo.Core.Date.FormatStyles = Xojo.Core.Date.FormatStyles.Medium) As Text

Supported for all project types and targets.

Converts the Date (along with the time component) to a text format using the optional locale and formats.

Notes

The default non-locale savvy version (loc is Nil) of ToText returns a date in the SQLDateTime format YYYY-MM-DD HH:MM:SS. When using a loc of Nil, the dateStyle and timeStyle must be FormatStyles.Medium, but since those are the defaults they can be omitted. Because of this there is no need to specifically pass in a loc of Xojo.Locale.Raw.

Example date output with no parameters would be: 2017-10-31 09:38:24

Use a Locale to get a date to use for display purposes. If you provide a Locale, then the specific output formats use what is defined in the locale settings for your OS. Refer to FormatStyles for more information.

Also refer to Localization in the User Guide for information on how you can display localized values, particularly with web apps.

Sample Code

Display the current date and time in SQLDateTime format (YYYY-MM-DD HH:MM:SS):

Var d As Xojo.Core.Date = Xojo.Core.Date.Now
Var SQLDateTime As Text = d.ToText // 2017-10-31 09:38:24

To get just the date portion you have to specify a Locale (not Raw). This example uses the current system locale to get just the date part for display:

Using Xojo.Core
Var d As Date = Date.Now
Var t As Text = d.ToText(Locale.Current, Date.FormatStyles.Short, Date.FormatStyles.None)
// t = 10/31/17 (this varies by date and your system locale settings)

To display just the time portion you have to specify a Locale and then provide a FormatStyle for the time: Using Xojo.Core

Var d As Date = Date.Now
Var t As Text = d.ToText(Locale.Current, Date.FormatStyles.None, Date.FormatStyles.Short)
// t = 12:24 PM (this varies by time and your system locale settings)