I'm not at all convinced that this is the best way to do it, but it works:
#include <time.h>
#include <string>
#include <sstream>
#include <iostream>
int main() {
time_t now = time(0);
tm *ltm = localtime(&now);
std::stringstream date;
date << ltm->tm_mday
<< "/"
<< 1 + ltm->tm_mon
<< "/"
<< 1900 + ltm->tm_year
<< " "
<< 1 + ltm->tm_hour
<< ":"
<< 1 + ltm->tm_min
<< ":"
<< 1 + ltm->tm_sec;
std::cout << date.str() << "\n";
}
The strftime()
function will do most of this work for you, but building up the parts of the string using a stringstream
may be more generally useful.