Monday, July 27, 2009

Displaying the date in C++?

I have a problem with showing only the date, and I can not find anything on the net that shows me how to show just the date. (Instead of the day, date, and time.) I know that you have to use struct tm, but I can not figure out how to just display one aspect of that.

Displaying the date in C++?
#include%26lt;time.h%26gt;


#include%26lt;iostream.h%26gt;


#include%26lt;stdio.h%26gt;


#include%26lt;conio.h%26gt;


void main()


{


time_t t;


time(%26amp;t);


cout%26lt;%26lt;ctime(%26amp;t);


getch();


}
Reply:You might have to cout the members of the tm struct individually..





cout %26lt;%26lt; tm.month %26lt;%26lt; "-" %26lt;%26lt; tm.day %26lt;%26lt; "-" ...





If told us your your platform and compiler we could probably nail it closer for you.





In some other langauges there are functions such as:





TodayDate.ToShortString();





That do that automatically, and take care of regional formation issues: ie myd or dmy ymd....
Reply:try this website.


http://www.codersource.net/cpp_date_time...
Reply:The members of struct tm are all int.


Use sprintf to construct the string you want then cout it.


No comments:

Post a Comment