Using Custom Format Strings in a Date and Time Picker Control

 

The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.

The latest version of this topic can be found at Using Custom Format Strings in a Date and Time Picker Control.

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, 2000".

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

  • 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 made up of either existing format characters, nonformat characters, or a combination of both. Once the custom string is built, make a call to CDateTimeCtrl::SetFormat 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_DateTimeCtrl.SetFormat(formatStr);

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

See Also

Using CDateTimeCtrl
Controls