Share via


Using Custom Format Strings in a Date and Time Picker Control

OverviewSample

By default, date and time picker controls provide three format types (each format corresponding to a unique style) for displaying the current date or time:

  • DTS_LONGDATEFORMAT   Displays the date in long format, producing output like "Wednesday, January 3, 1998".

  • DTS_SHORTDATEFORMAT   Displays the date in short format, producing output like "1/3/98".

  • DTS_TIMEFORMAT   Displays the time in long format, producing output like "5:31:42 PM".

However, you can customize the appearance of the date or time by using a custom format string. This custom string is comprised of either existing format characters, nonformat characters, or a combination of both. Once the custom string is built, make a call to passing in your custom string. The date and time picker control will then display the current value using your custom format string.

The following example code (where m_dtPicker is the CDateTimeCtrl object) demonstrates one possible solution:

CString formatStr= _T("'Today is: 'yy'/'MM'/'dd");
m_dtPicker.SetFormat(formatStr);

In addition to custom format strings, date and time picker controls also support callback fields.

See Also   Windows Common Controls and MFC Classes