?
Solved

Date Formatting

Posted on 2003-03-19
5
Medium Priority
?
154 Views
Last Modified: 2010-04-01
I have written a program in Visual C++ that has a DateTimePicker and a button.
When I click the button In want the date that has been selected in the DTPicker to
be shown in a message box in the format of dd/mm/yy.

I am new to VC++ and any help would be greatly appreciated.

Thanks
0
Comment
Question by:HotRod40
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 12

Accepted Solution

by:
Salte earned 200 total points
ID: 8166984
As far as I know the DateTimePicker return the data in a CDateTime class. This class has a formatting function where you can specify format such as "dd/mm/YY". However, it is possible that 'dd', 'mm' and 'yy' cannot be specified as such. It is possible that you must write "YY" instead of "yy" etc. Also, to make matters worse I think this format depends on locale so if you're in a country other than US or english speaking country the code for year might not be "yy" but some other letter instead etc.

Essentially the CDateTime::Format() or whatever the function is called takes a format string similar to the string provided in your locale setup and similar to the format given in date time formats for Excel or the VB variant data type.

Check the class CDateTime or whatever data type the DateTimePicker returns and see what functions are in it. Use the online documentation so just set cursor on DateTimePicker and press F1 and follow the hyperlinks from there.

Alf
0
 
LVL 48

Assisted Solution

by:AlexFM
AlexFM earned 200 total points
ID: 8167288
void CMyClass::OnButtonPressed()
{
    SYSTEMTIME time;

    m_date_time_picker.GetTime(&time);

    CString s;
    s.Format(_T("%02d/%02d/%02d"),
             time.wDay, time.wMonth, time.wYear);

    MessageBox(s);
}

0
 
LVL 2

Expert Comment

by:bkrahmer
ID: 8172066
AlexFM, you seem to have ignored Salte's statements about localization.  The GetDateFormat function formats a date as a date string for a specified locale.  Check that out.

brian
0
 
LVL 12

Expert Comment

by:Salte
ID: 8173165
Anyway, if HotRod40 wants it in the specific format dd/mm/yy the method shown by AlexFM is one way to do it.

However, be aware that if your program is to be locale dependent you should NOT use that format. In that case the format should probably be "according to locale" and so you should rather use whatever format the locale tells you to use. MFC has functions to automatically format a time "according to locale" and you should most probably use those functions instead.

Alf
0
 
LVL 9

Expert Comment

by:tinchos
ID: 9510629
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

Split points between Salte & AlexFM

Please leave any comments here within the next seven days.

PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!

Tinchos
EE Cleanup Volunteer
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
Suggested Courses

741 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question