[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 158
  • Last Modified:

Date Formatting

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
HotRod40
Asked:
HotRod40
2 Solutions
 
SalteCommented:
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
 
AlexFMCommented:
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
 
bkrahmerCommented:
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
 
SalteCommented:
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
 
tinchosCommented:
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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now