formatting time

How can I get C++ to output time in this format?
8/2/2009 16:07

Well... there are a few different ways you could do it that I guess would depend on what you want to do with the data later. Assuming you don't need that format stored anywhere a simple method would just be a series of:

 
cout << month << "/" << day << "/" << year << " " <<  hour << ":" << minute;


This of course does not take into account several factors like ensuring that 07 outputs as 07 and not just 7, etc...
This program generates a .csv that I can upload to my website's database. The PHP code on my site need it to me in 8/2/2009 16:07 format.
So you just need to dump the date and time into a csv file in the desired format. Take a look at fstream. http://www.cplusplus.com/reference/iostream/fstream/
I got that, but thanks. The program is compiled and works great. Now I'm finishing it up and making it a little more functional. ATM, my program is programed to output "8/2/2009 16:07" as the date/time. I would really like it to output the current date/time instead.
:
I found a code online but it outputs MM:DD:YY HH:MM:SS not MM:DD:YYYY HH:MM:SS
1
2
3
4
5
6
7
8
9
10
11
12
13
14

 #include <stdio.h>
     #include <time.h>

     int main( )
     {
          char dateStr [9];
          char timeStr [9];
          _strdate( dateStr);
          printf( "The current date is %s \n", dateStr);
         _strtime( timeStr );
         printf( "The current time is %s \n", timeStr);
      }


Am I just being too picky?
You weren't too clear on what you needed in your first post. Here's how you can get the current date, time, & day of the week in Windows:

1
2
3
4
5
6
7
8
9
#include <windows.h>

int main()
{
	SYSTEMTIME st;
	GetSystemTime(&st);

	return 0;
}


Struct is documented here.
http://msdn2.microsoft.com/en-us/library/ms724950.aspx
Last edited on
Sorry about that. I thought it was clear but I guess it wasn't.

Thanks Return, your link was very helpful. [
code]
#include <windows.h>
#include <stdio.h>

int main()
{
SYSTEMTIME st, lt;

GetSystemTime(&st);
GetLocalTime(&lt);

printf("The system time is: %02d:%02d:%02d %02d:%02d\n", st.wMonth, st.wDay, st.wYear, st.wHour, st.wMinute);
;
}
[/code]
Topic archived. No new replies allowed.