Click here to Skip to main content
Licence CPOL
First Posted 6 Feb 2003
Views 91,939
Downloads 117
Bookmarked 34 times

A Date and Time formatter

By | 19 Feb 2003 | Article
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)

About the Author

Joao Paulo Figueira

Software Developer
Primeworks
Portugal Portugal

Member

João is a partner at Primeworks, a company that develops remote database access software for Windows Mobile. He also works for Frotcom, a company that develops web-based fleet management solutions.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralWrong weeknumber Pinsusstom.schultz1:43 3 Feb '05  
GeneralRe: Wrong weeknumber PinmemberJoão Paulo Figueira3:10 3 Feb '05  
GeneralFix Pinsusstom.schultz21:46 7 Apr '05  
GeneralRe: Fix PinmemberJoão Paulo Figueira21:55 7 Apr '05  
GeneralRe: Fix Pinmembertom.schultz1:34 20 Mar '06  
Questionhow to get 'seconds' ? Pinmemberriki_risnandar17:20 1 Jan '05  
AnswerRe: how to get 'seconds' ? PinmemberJoão Paulo Figueira0:41 2 Jan '05  
GeneralDate time picker Win 32 API PinsussAnonymous19:59 22 Apr '03  
GeneralRe: Date time picker Win 32 API PinmemberSiyaulHaqu4:21 13 Aug '07  
GeneralANSI C strftime() does this too.... PinmemberHarald Krause0:28 7 Feb '03  
GeneralRe: ANSI C strftime() does this too.... PinmemberJoão Paulo Figueira0:38 7 Feb '03  
GeneralRe: ANSI C strftime() does this too.... PinmemberBrian van der Beek9:17 9 Feb '03  
GeneralRe: ANSI C strftime() does this too.... PinmemberJoão Paulo Figueira10:32 9 Feb '03  
GeneralRe: ANSI C strftime() does this too.... PinmemberJohn A. Johnson2:36 7 Feb '03  
GeneralRe: ANSI C strftime() does this too.... PinmemberHarald Krause2:44 7 Feb '03  
GeneralRe: ANSI C strftime() does this too.... PinmemberJoão Paulo Figueira4:15 7 Feb '03  
GeneralRe: ANSI C strftime() does this too.... PinmemberSteve Mayfield19:40 7 Feb '03  
GeneralRe: ANSI C strftime() does this too.... PinmemberJoão Paulo Figueira6:11 8 Feb '03  

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.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120528.1 | Last Updated 20 Feb 2003
Article Copyright 2003 by Joao Paulo Figueira
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid