[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
Solved

How change format of date in MFC

Posted on 2010-08-19
10
Medium Priority
?
1,009 Views
Last Modified: 2012-05-10
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,DATE_SHORTDATE,&tm,0,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
string.format("MM-dd-yyyy",08/19/2010);

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
0
Comment
Question by:Rajkumar Gs
[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
10 Comments
 
LVL 40

Accepted Solution

by:
evilrix earned 2000 total points
ID: 33472671
Have you seem the format string that can be passed?

http://msdn.microsoft.com/en-us/library/ms905235.aspx

int GetDateFormat(
  LCID Locale,
  DWORD dwFlags,
  CONST SYSTEMTIME* lpDate,
  LPCTSTR lpFormat,  <--------------- format string
  LPTSTR lpDateStr,
  int cchDate
);

lpFormat

[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.
0
 
LVL 2

Expert Comment

by:Aamir Sarfaraz
ID: 33472692
You can use these code and format as per your requirments.

NOTE: DON'T FORGET TO CLICK "HELPFUL COMMENTS YES"
#include <Windows.h>
     #include <stdio.h>

     void main()
     {
         SYSTEMTIME st;
         GetSystemTime(&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

0
 
LVL 33

Expert Comment

by:pgnatyuk
ID: 33472825
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.
0
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!

 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33473029
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
Thanks
0
 
LVL 40

Expert Comment

by:evilrix
ID: 33473035
>> 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);
0
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33473052
I try your code. But getting the same error,

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

Thanks
0
 
LVL 40

Expert Comment

by:evilrix
ID: 33473071
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?
0
 
LVL 23

Author Comment

by:Rajkumar Gs
ID: 33473083
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. :-)
0
 
LVL 23

Author Closing Comment

by:Rajkumar Gs
ID: 33473097
This is a good solution and save my time. Thanks.
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Unlike C#, C++ doesn't have native support for sealing classes (so they cannot be sub-classed). At the cost of a virtual base class pointer it is possible to implement a pseudo sealing mechanism The trick is to virtually inherit from a base class…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

656 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