在记录程序日志时,需要记录时间。如下:

#include <iostream>
#include <time.h>
#include <windows.h>
using namespace std;
int main() {
    time_t t = time(0);
    char tmp[64];
    strftime(tmp, sizeof(tmp), "%Y/%m/%d %X %A %j %z", localtime(&t));
    cout << tmp << endl;
    system("pause");
    return 0;
}

即Y为年、m为月、d为日、X为具体时分秒、A为星期、j为天数、z为其他,结果如下:

2018/06/04 19:52:54 Monday 155 China Standard Time

 

 

如果通过函数返回,需要这样:

char* getTime()
{
    time_t t = time(0);
    strftime(tmp, sizeof(tmp), "%Y/%m/%d %X", localtime(&t));
    return tmp;
}

其中,char tmp[64];定义为全局变量即可,然后直接调用。

 

相关文章:

  • 2022-12-23
  • 2021-09-30
  • 2022-12-23
  • 2021-09-30
  • 2022-01-24
  • 2021-09-19
  • 2021-11-30
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-09-30
  • 2022-12-23
  • 2022-12-23
  • 2021-11-08
  • 2021-08-27
相关资源
相似解决方案