How to show date and time with cout manipulator in C++

1 Answer

0 votes
#include <iostream>
#include <ctime>

using namespace std;

ostream &datetime(ostream &stream)
{
	struct tm *localt;
	time_t t;

	t = time(NULL);
	localt = localtime(&t);
	stream << asctime(localt) << endl;

	return stream;
}

int main()
{
	cout << datetime << endl;

	return 0;
}


/*
run:

Mon Apr  9 08:40:26 2018

*/

 



answered Apr 9, 2018 by avibootz

Related questions

1 answer 166 views
1 answer 181 views
1 answer 212 views
1 answer 314 views
...