Click here to Skip to main content
12,634,712 members (24,949 online)
Click here to Skip to main content
Add your own
alternative version

Stats

103.1K views
295 downloads
34 bookmarked
Posted

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:

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)

Share

About the Author

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

You may also be interested in...

Pro
Pro

Comments and Discussions

 
GeneralWrong weeknumber Pin
tom.schultz3-Feb-05 2:43
susstom.schultz3-Feb-05 2:43 
GeneralRe: Wrong weeknumber Pin
João Paulo Figueira3-Feb-05 4:10
memberJoão Paulo Figueira3-Feb-05 4:10 
GeneralFix Pin
tom.schultz7-Apr-05 22:46
susstom.schultz7-Apr-05 22:46 
GeneralRe: Fix Pin
João Paulo Figueira7-Apr-05 22:55
memberJoão Paulo Figueira7-Apr-05 22:55 
GeneralRe: Fix Pin
tom.schultz20-Mar-06 2:34
membertom.schultz20-Mar-06 2:34 
Questionhow to get 'seconds' ? Pin
riki_risnandar1-Jan-05 18:20
memberriki_risnandar1-Jan-05 18:20 
AnswerRe: how to get 'seconds' ? Pin
João Paulo Figueira2-Jan-05 1:41
memberJoão Paulo Figueira2-Jan-05 1:41 
GeneralDate time picker Win 32 API Pin
Anonymous22-Apr-03 20:59
sussAnonymous22-Apr-03 20:59 
GeneralRe: Date time picker Win 32 API Pin
SiyaulHaqu13-Aug-07 5:21
memberSiyaulHaqu13-Aug-07 5:21 
GeneralANSI C strftime() does this too.... Pin
Harald Krause7-Feb-03 1:28
memberHarald Krause7-Feb-03 1:28 
GeneralRe: ANSI C strftime() does this too.... Pin
João Paulo Figueira7-Feb-03 1:38
memberJoão Paulo Figueira7-Feb-03 1:38 
GeneralRe: ANSI C strftime() does this too.... Pin
Brian van der Beek9-Feb-03 10:17
memberBrian van der Beek9-Feb-03 10:17 
GeneralRe: ANSI C strftime() does this too.... Pin
João Paulo Figueira9-Feb-03 11:32
memberJoão Paulo Figueira9-Feb-03 11:32 
GeneralRe: ANSI C strftime() does this too.... Pin
John A. Johnson7-Feb-03 3:36
memberJohn A. Johnson7-Feb-03 3:36 
GeneralRe: ANSI C strftime() does this too.... Pin
Harald Krause7-Feb-03 3:44
memberHarald Krause7-Feb-03 3:44 
GeneralRe: ANSI C strftime() does this too.... Pin
João Paulo Figueira7-Feb-03 5:15
memberJoão Paulo Figueira7-Feb-03 5:15 
GeneralRe: ANSI C strftime() does this too.... Pin
Steve Mayfield7-Feb-03 20:40
memberSteve Mayfield7-Feb-03 20:40 
GeneralRe: ANSI C strftime() does this too.... Pin
João Paulo Figueira8-Feb-03 7:11
memberJoão Paulo Figueira8-Feb-03 7: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.

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