Click here to Skip to main content
15,885,537 members
Articles / Mobile Apps / Windows Mobile

A Date and Time formatter

Rate me:
Please Sign up or sign in to vote.
4.56/5 (9 votes)
19 Feb 2003CPOL3 min read 125.5K   415   35   18
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:

ElementDescription
dThe 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.
ddddThe full weekday name.
wSingle or double digit week number.
ww Double digit week number.
hThe 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.
MThe one- or two-digit month number.
MMThe two-digit month number. Single-digit values are preceded by a zero.
MMMThe three-character month abbreviation.
MMMMThe full month name.
q Single digit quarter number.
t The one-letter AM/PM abbreviation (that is, AM is displayed as "A").
ttThe 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)


Written By
Software Developer (Senior) Frotcom International
Portugal Portugal
I work on R&D for Frotcom International, a company that develops web-based fleet management solutions.

Comments and Discussions

 
GeneralWrong weeknumber Pin
tom.schultz3-Feb-05 1:43
tom.schultz3-Feb-05 1:43 
GeneralRe: Wrong weeknumber Pin
João Paulo Figueira3-Feb-05 3:10
professionalJoão Paulo Figueira3-Feb-05 3:10 
GeneralFix Pin
tom.schultz7-Apr-05 21:46
tom.schultz7-Apr-05 21:46 
GeneralRe: Fix Pin
João Paulo Figueira7-Apr-05 21:55
professionalJoão Paulo Figueira7-Apr-05 21:55 
GeneralRe: Fix Pin
tom.schultz20-Mar-06 1:34
tom.schultz20-Mar-06 1:34 
Questionhow to get 'seconds' ? Pin
riki_risnandar1-Jan-05 17:20
riki_risnandar1-Jan-05 17:20 
AnswerRe: how to get 'seconds' ? Pin
João Paulo Figueira2-Jan-05 0:41
professionalJoão Paulo Figueira2-Jan-05 0:41 
GeneralDate time picker Win 32 API Pin
Anonymous22-Apr-03 19:59
Anonymous22-Apr-03 19:59 
GeneralRe: Date time picker Win 32 API Pin
SiyaulHaqu13-Aug-07 4:21
SiyaulHaqu13-Aug-07 4:21 
GeneralANSI C strftime() does this too.... Pin
Harald Krause7-Feb-03 0:28
Harald Krause7-Feb-03 0:28 
GeneralRe: ANSI C strftime() does this too.... Pin
João Paulo Figueira7-Feb-03 0:38
professionalJoão Paulo Figueira7-Feb-03 0:38 
GeneralRe: ANSI C strftime() does this too.... Pin
Brian van der Beek9-Feb-03 9:17
Brian van der Beek9-Feb-03 9:17 
GeneralRe: ANSI C strftime() does this too.... Pin
João Paulo Figueira9-Feb-03 10:32
professionalJoão Paulo Figueira9-Feb-03 10:32 
GeneralRe: ANSI C strftime() does this too.... Pin
andyj1157-Feb-03 2:36
andyj1157-Feb-03 2:36 
Different objectves: in my program code I'll use the "strftime()" function that is more powerfull, but if I have to ask the format to the end user, I prefer this article solution because is more similar to the Excel/Access style format.

And, because I really have to create a "properties" dialog in my project with the display format preference... I'll use this article for sure!

Big Grin | :-D

Ps: Thank you João Paulo Figueira!
GeneralRe: ANSI C strftime() does this too.... Pin
Harald Krause7-Feb-03 2:44
Harald Krause7-Feb-03 2:44 
GeneralRe: ANSI C strftime() does this too.... Pin
João Paulo Figueira7-Feb-03 4:15
professionalJoão Paulo Figueira7-Feb-03 4:15 
GeneralRe: ANSI C strftime() does this too.... Pin
Steve Mayfield7-Feb-03 19:40
Steve Mayfield7-Feb-03 19:40 
GeneralRe: ANSI C strftime() does this too.... Pin
João Paulo Figueira8-Feb-03 6:11
professionalJoão Paulo Figueira8-Feb-03 6:11 

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

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