Click here to Skip to main content
Click here to Skip to main content

A Date and Time formatter

, 19 Feb 2003 CPOL
Rate this:
Please Sign up or sign in to vote.
A class to format COleDateTime objects into strings using standard formatting codes.

Introduction

This class formats into strings COleDateTime objects. It uses the commonly defined formatting options such as MM-dd-yy or hh:mm:ss.

Background

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 CDateTimeFormat object:

CDateTimeFormat dtf;

This object may be used for more than one format. The second thing is to set the COleDateTime object:

dtf.SetDateTime(odtDateTime);

This method takes a reference for speed. Next, you set the format string:

dtf.SetFormat(_T("MM-dd-yyyy"));

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:

TCHAR szMyBuffer[256];
dtf.SetFormatBuf(szMyBuffer);

You should allocate enough space for the format strings. The default allocation in the format object is 256 characters.

Formatting syntax

The formatting syntax is based on the DateTimePicker control. The following table (transcribed from MSDN) contains all the supported formatting controls:

Element Description
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).

Locale usage

This class makes heavy use of the GetLocaleInfo API, using the LOCALE_SYSTEM_DEFAULT. This can be changed to match your needs.

Speed optimizations

Speed may be optimized by pre-caching all locale data on the first instantiation. This will be added in the next release.

Revisions

  • 2003-02-20: Added support for week number and quarter number formatting.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

João Paulo Figueira
Software Developer Frotcom International
Portugal Portugal
I work on R&D for Frotcom International, a company that develops web-based fleet management solutions.
Follow on   Twitter   LinkedIn

Comments and Discussions

 
GeneralWrong weeknumber Pinsusstom.schultz3-Feb-05 2:43 
GeneralRe: Wrong weeknumber PinmemberJoão Paulo Figueira3-Feb-05 4:10 
GeneralFix Pinsusstom.schultz7-Apr-05 22:46 
GeneralRe: Fix PinmemberJoão Paulo Figueira7-Apr-05 22:55 
GeneralRe: Fix Pinmembertom.schultz20-Mar-06 2:34 
Questionhow to get 'seconds' ? Pinmemberriki_risnandar1-Jan-05 18:20 
AnswerRe: how to get 'seconds' ? PinmemberJoão Paulo Figueira2-Jan-05 1:41 
GeneralDate time picker Win 32 API PinsussAnonymous22-Apr-03 20:59 
GeneralRe: Date time picker Win 32 API PinmemberSiyaulHaqu13-Aug-07 5:21 
GeneralANSI C strftime() does this too.... PinmemberHarald Krause7-Feb-03 1:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.141223.1 | Last Updated 20 Feb 2003
Article Copyright 2003 by João Paulo Figueira
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid