Xbasic

DTOC Function

Syntax

dim Formatted_Date as C = DTOC(Date_Value D [, Format as C [, Localized as L]])

dim Formatted_Date as C = DTOC(Date_Value as T [, Format as C])

Arguments

Date_ValueDate Time

A variable containing a date or date time value.

FormatLogical

Default = "MM/DD/YYYY". Refer to Predefined Date Formats below.

localizedLogical

Default = .f.. If true, the date is localized using the regional language setting. This only applies to formats where the name of the month is printed.

Returns

Formatted_DateCharacter

The Date_Value formatted into a character string.

Description

Converts a date into a character value in the form 'MM/DD/YYYY'.

Discussion

DTOC() returns a character string in the specified Format for the specified Date or Date_Time value. DTOC() is useful for indexing a table when month order is more important than chronological date order (which orders first by year, then month, then day).

If you have a multi-field index involving both date and character fields, use the CDATE() function if you want a chronological date order.

Predefined Date Formats

The following formats can be used with the DTOC() function.

Format Code

Meaning

1-

12-31-2015

2-

21-12-2015

1/

12/31/2015

2/

31/12/2015

3

31 December 2015

4

December 31, 2015

8

December 31st, 2015

5-

12-15

6-

12-31-15

7-

31-12-15

5/

12/15

6/

12/31/15

7/

31/12/15

9-

31-Dec-15

10

31 Dec 2015

11-

15-12-31

12-

2015-12-31

11/

15/12/31

12/

2015/12/31

11.

15.12.31

12.

2015.12.31

3C

31 DECEMBER 2015

4C

DECEMBER 31, 2015

8C

DECEMBER 31ST, 2015

9C-

31-DEC-15

10C

31 DEC 2015

Examples

dim now as D
now = now()
? now
= {12/12/2017}

? dtoc(now)
= "12/12/2017"

? dtoc(now,"12.")
= "2017.12.12"

? dtoc(now,"10",.f.)
= "12 Dec 2017"

' System Locale is set to France (Belgium):
? dtoc(now,"10",.t.)
= "12 déc 2017"

dim now2 as T
now2 = now()
? now2
= 12/12/2017 10:58:20 01 am

? dtoc(now2)
= "12/12/2017"

? dtoc(now2,"6-")
= "12-12-17"

? dtoc(now2,"4C",.f.)
= "DECEMBER 12, 2017"

' System Locale is set to France (Belgium):
? dtoc(now2,"4C",.t.)
= "DéCEMBRE 12, 2017"

See Also