This class formats into strings
COleDateTime objects. It uses the commonly defined formatting options such as MM-dd-yy or hh:mm:ss.
There is no single Win32 API function that will handle both date and time formatting in the same place. The Win32 API supports the
GetDateFormat and the
GetTimeFormat functions, but they have to be used separately. Worse, if the programmer wants to provide the user with a single date and time formatting string, he or she will have to split it and provide each function, its part of the format string. This class was created to address this issue.
Using the class
The class is very simple to use. Start by instantiating a
This object may be used for more than one format. The second thing is to set the
This method takes a reference for speed. Next, you set the format string:
Now, you only need to get the formatted string, using:
CString strFormat = dtf.GetString();
If you do not want to pay for a
CString copy when retrieving the formatted string, you can also give the object a pointer to your
TCHAR buffer by using:
You should allocate enough space for the format strings. The default allocation in the format object is 256 characters.
The formatting syntax is based on the
DateTimePicker control. The following table (transcribed from MSDN) contains all the supported formatting controls:
||The one- or two-digit day.
||The two-digit day. Single-digit day values are preceded by a zero.
||The three-character weekday abbreviation.
||The full weekday name.
||Single or double digit week number.
||Double digit week number.
||The one- or two-digit hour in 12-hour format.
||The two-digit hour in 12-hour format. Single-digit values are preceded by a zero.
||The one- or two-digit hour in 24-hour format.
||The two-digit hour in 24-hour format. Single-digit values are preceded by a zero.
||The one- or two-digit minute.
||The two-digit minute. Single-digit values are preceded by a zero.
||The one- or two-digit month number.
||The two-digit month number. Single-digit values are preceded by a zero.
||The three-character month abbreviation.
||The full month name.
||Single digit quarter number.
||The one-letter AM/PM abbreviation (that is, AM is displayed as "A").
||The two-letter AM/PM abbreviation (that is, AM is displayed as "AM").
||The last two digits of the year (that is, 1996 would be displayed as "96").
||The full year (that is, 1996 would be displayed as "1996").
||Enter literal mode. All characters are copied to the format string up to the next single quote.
||Enter literal mode for the next character only (escape).
This class makes heavy use of the
GetLocaleInfo API, using the
LOCALE_SYSTEM_DEFAULT. This can be changed to match your needs.
Speed may be optimized by pre-caching all locale data on the first instantiation. This will be added in the next release.
- 2003-02-20: Added support for week number and quarter number formatting.