std::cout to std::string

Hi,

I need my script to store a date as a variable.
The current script I have to generate the date is working.
The output generated is, for example: 2015-9-26

Now I need it to store the generated output to a variable.
To my knowledge, a string will do best for this but my compiler won't allow this. (Visual Studio on 2015)

Why doesn't it allow this to be stored as a string?
It states that the operator doens't match the operand, but when I try to enclose it () it also doesn't work.

string date;
date = cout << (now->tm_year + 1900) << '-'
<< (now->tm_mon + 1) << '-'
<< now->tm_mday
<< endl;

1
2
3
4
5
6
7
  	using namespace std;
	time_t t = time(0);   // get time now
    	struct tm * now = localtime( & t );
    	cout << (now->tm_year + 1900) << '-' 
         << (now->tm_mon + 1) << '-'
         <<  now->tm_mday
         << endl;
Last edited on
closed account (48T7M4Gy)
http://www.cplusplus.com/reference/ctime/asctime/
Thanks a lot! Got it working!
Topic archived. No new replies allowed.