您的位置:首页 > 编程语言 > C语言/C++

C/C++代码获取当前时间的:年月日时分秒

2017-12-24 16:30 411 查看
       直接上菜:

#include <iostream>
#include <string>
using namespace std;

static string getCurrentTimeStr()
{
time_t t = time(NULL);
char ch[64] = {0};
strftime(ch, sizeof(ch) - 1, "%Y-%m-%d %H:%M:%S", localtime(&t)); //年-月-日 时-分-秒
return ch;
}

int main()
{
cout << getCurrentTimeStr() << endl;
return 0;
}       结果:
ubuntu@VM-0-13-ubuntu:~$ ./a.out
2017-12-24 16:31:17
ubuntu@VM-0-13-ubuntu:~$ date
Sun Dec 24 16:31:19 CST 2017
ubuntu@VM-0-13-ubuntu:~$

      good
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: