How change format of date in MFC


I am getting the current date in MFC application using the below code

TCHAR date[32];

format is : 08/19/2010. But i need to change the format like 08-19-2010. In C# i have the code like

But in MFC what method and exact code writing to change. I am new in C++ and MFC. Please suggest me.Hope your's reply

LVL 23
Rajkumar GsSoftware EngineerAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

evilrixSenior Software Engineer (Avast)Commented:
Have you seem the format string that can be passed?

int GetDateFormat(
  LCID Locale,
  DWORD dwFlags,
  LPCTSTR lpFormat,  <--------------- format string
  LPTSTR lpDateStr,
  int cchDate


[in] Pointer to a format picture string to use to form the date string. If lpFormat is NULL, the function uses the date format of the specified locale.
Use the following elements to construct a format picture string. If you use spaces to separate the elements in the format string, these spaces will appear in the same location in the output string. The letters must be in uppercase or lowercase as shown in the table (for example, "MM" not "mm"). Characters in the format string that are enclosed in single quotation marks will appear in the same location and unchanged in the output string.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Aamir SarfarazIT ManagerCommented:
You can use these code and format as per your requirments.

#include <Windows.h>
     #include <stdio.h>

     void main()
         SYSTEMTIME st;
         printf("Year:%d\nMonth:%d\nDate:%d\nHour:%d\nMin:%d\nSecond:% d\n" ,st.wYear,st.wMonth,st.wDay,st.wHour,st.wMinute,st.wSecond);

Open in new window

In MFC there is a very powerfull class CTime:
It contains Format method:
You will see the example in the bottom of that page.
In the code you posted you use Win32 API and only 'string' from MFC. I like Win32 API, I complain a lot about MFC, but CTime is an exception - it is easy, it always works.
Build an E-Commerce Site with Angular 5

Learn how to build an E-Commerce site with Angular 5, a JavaScript framework used by developers to build web, desktop, and mobile applications.

Rajkumar GsSoftware EngineerAuthor Commented:
hi evilrix,

 You r right.I used that function. But how can i get it? that means, how i get the value? I put
CString returnvalu = GetDateFormat(LOCALE_SYSTEM_DEFAULT,0,NULL,"ddd'-'MMM'-'yyyy",date,32);
But it give the error that Error      1      error C2440: 'initializing' : cannot convert from 'int' to 'ATL::CStringT<BaseType,StringTraits>'      . How can i assign the string and look the output. Hope your's reply
evilrixSenior Software Engineer (Avast)Commented:
>> CString returnvalu = GetDateFormat(LOCALE_SYSTEM_DEFAULT,0,NULL,"ddd'-'MMM'-'yyyy",date,32);

try: CString returnvalu = GetDateFormat(LOCALE_SYSTEM_DEFAULT,0,NULL,_T("ddd'-'MMM'-'yyyy"),date,32);
Rajkumar GsSoftware EngineerAuthor Commented:
I try your code. But getting the same error,

Error      1      error C2440: 'initializing' : cannot convert from 'int' to 'ATL::CStringT<BaseType,StringTraits>'      

evilrixSenior Software Engineer (Avast)Commented:
And the error goes away if you change the code back to...

CString returnvalu = GetDateFormat(LOCALE_SYSTEM_DEFAULT,0,NULL,0,date,32);


That error makes little sense in the context of the change made.

Does the error refer to that line number? What I'm getting at here is are you sure the error is related to this change?
Rajkumar GsSoftware EngineerAuthor Commented:
hi, i got it. sorry i misunderstand that the function how it is working? I thought that like c# that get current date and pass it in string.format("MM-dd-yyyy",currentdate); But here is easy to get from getdateformat() function that is i got a output from parameter from lpDateStr. This is output what i need. Thanks dude.

I got the solution. :-)
Rajkumar GsSoftware EngineerAuthor Commented:
This is a good solution and save my time. Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.