Rajkumar Gs
asked on
How change format of date in MFC
Hi,
I am getting the current date in MFC application using the below code
SYSTEMTIME tm;
TCHAR date[32];
GetLocalTime(&tm);
GetDateFormat(LOCALE_USER_ DEFAULT,DA TE_SHORTDA TE,&tm,0,d ate,32);
format is : 08/19/2010. But i need to change the format like 08-19-2010. In C# i have the code like
string.format("MM-dd-yyyy" ,08/19/201 0);
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
Thanks
I am getting the current date in MFC application using the below code
SYSTEMTIME tm;
TCHAR date[32];
GetLocalTime(&tm);
GetDateFormat(LOCALE_USER_
format is : 08/19/2010. But i need to change the format like 08-19-2010. In C# i have the code like
string.format("MM-dd-yyyy"
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
Thanks
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
In MFC there is a very powerfull class CTime:
http://msdn.microsoft.com/en-us/library/78zb0ese(VS.80).aspx
It contains Format method:
http://msdn.microsoft.com/en-us/library/29btb3sw(v=VS.80).aspx
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.
http://msdn.microsoft.com/en-us/library/78zb0ese(VS.80).aspx
It contains Format method:
http://msdn.microsoft.com/en-us/library/29btb3sw(v=VS.80).aspx
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.
ASKER
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_SYSTE M_DEFAULT, 0,NULL,"dd d'-'MMM'-' yyyy",date ,32);
But it give the error that Error 1 error C2440: 'initializing' : cannot convert from 'int' to 'ATL::CStringT<BaseType,St ringTraits >' . How can i assign the string and look the output. Hope your's reply
Thanks
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_SYSTE
But it give the error that Error 1 error C2440: 'initializing' : cannot convert from 'int' to 'ATL::CStringT<BaseType,St
Thanks
>> CString returnvalu = GetDateFormat(LOCALE_SYSTE M_DEFAULT, 0,NULL,"dd d'-'MMM'-' yyyy",date ,32);
try: CString returnvalu = GetDateFormat(LOCALE_SYSTE M_DEFAULT, 0,NULL,_T( "ddd'-'MMM '-'yyyy"), date,32);
try: CString returnvalu = GetDateFormat(LOCALE_SYSTE
ASKER
I try your code. But getting the same error,
Error 1 error C2440: 'initializing' : cannot convert from 'int' to 'ATL::CStringT<BaseType,St ringTraits >'
Thanks
Error 1 error C2440: 'initializing' : cannot convert from 'int' to 'ATL::CStringT<BaseType,St
Thanks
And the error goes away if you change the code back to...
CString returnvalu = GetDateFormat(LOCALE_SYSTE M_DEFAULT, 0,NULL,0,d ate,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?
CString returnvalu = GetDateFormat(LOCALE_SYSTE
??
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?
ASKER
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" ,currentda te); 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. :-)
I got the solution. :-)
ASKER
This is a good solution and save my time. Thanks.
NOTE: DON'T FORGET TO CLICK "HELPFUL COMMENTS YES"
Open in new window