1 #include <string> 2 #include <time.h> 3 using namespace std; 4 5 string getTime() 6 { 7 time_t timep; 8 time (&timep); 9 char tmp[64]; 10 strftime(tmp, sizeof(tmp), "%Y-%m-%d %H:%M:%S",localtime(&timep) ); 11 return tmp; 12 } 13 14 int main(){ 15 string time = getTime(); 16 cout << time << endl; 17 18 return 0; 19 }
输出结果为:

本文介绍了一种使用C++获取当前时间并将其格式化为字符串的方法。通过调用time.h库中的函数,可以将时间戳转换为本地时间,并进一步转化为YYYY-MM-DD HH:MM:SS格式的字符串。
1190

被折叠的 条评论
为什么被折叠?



