DateTime.ToString

From Xojo Documentation

You are currently browsing the old Xojo documentation site. Please visit the new Xojo documentation site!
Method

DateTime.ToString(loc As Locale = Nil, dateStyle As DateTime.FormatStyles = DateTime.FormatStyles.Medium, timeStyle As DateTime.FormatStyles = DateTime.FormatStyles.Medium) As String

New in 2019r2

Supported for all project types and targets.

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

Notes

If you don't provide a locale or use a Nil locale, then the current locale (Locale.Current) is used.

To get date values without locale information, use SQLDate or SQLDateTime.

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 UserGuide:Localization for information on how you can display localized values, particularly with web apps.

Sample Code

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:

Var d As DateTime = DateTime.Now
Var s As String = d.ToString(Locale.Current, DateTime.FormatStyles.Short, DateTime.FormatStyles.None)
// s = 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:

Var d As DateTime = DateTime.Now
Var s As String = d.ToString(Locale.Current, DateTime.FormatStyles.None, DateTime.FormatStyles.Short)
// s = 12:24 PM (this varies by time and your system locale settings)