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:
|d||The one- or two-digit day.|
|dd ||The two-digit day. Single-digit day values are preceded by a zero.|
|ddd ||The three-character weekday abbreviation.|
|dddd||The full weekday name.|
|w||Single or double digit week number.|
|ww ||Double digit week number.|
|h||The one- or two-digit hour in 12-hour format.|
|hh ||The two-digit hour in 12-hour format. Single-digit values are preceded by a zero.|
|H ||The one- or two-digit hour in 24-hour format.|
|HH ||The two-digit hour in 24-hour format. Single-digit values are preceded by a zero.|
|m ||The one- or two-digit minute.|
|mm ||The two-digit minute. Single-digit values are preceded by a zero.|
|M||The one- or two-digit month number.|
|MM||The two-digit month number. Single-digit values are preceded by a zero.|
|MMM||The three-character month abbreviation.|
|MMMM||The full month name.|
|q ||Single digit quarter number.|
|t ||The one-letter AM/PM abbreviation (that is, AM is displayed as "A").|
|tt||The two-letter AM/PM abbreviation (that is, AM is displayed as "AM").|
|yy ||The last two digits of the year (that is, 1996 would be displayed as "96").|
|yyyy ||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.