C++ 第一种方法 用strftime()
struct tm t; //tm结构指针
time_t now; //声明time_t类型变量
time(&now); //获取系统日期和时间
localtime_s(&t, &now); //获取当地日期和时间
char str[50];
strftime(str, sizeof(str), "%Y-%m-%d %H:%M:%S", &t);//格式化时间
printf(str);
printf("\n");
C++ 第二种方法
struct tm t; //tm结构指针
time_t now; //声明time_t类型变量
time(&now); //获取系统日期和时间
localtime_s(&t, &now); //获取当地日期和时间
//格式化输出本地时间
printf("年:%d\n", t.tm_year + 1900);
printf("月:%d\n", t.tm_mon + 1);
printf("日:%d\n", t.tm_mday);
printf("周:%d\n", t.tm_wday);
printf("一年中:%d\n", t.tm_yday);
printf("时:%d\n", t.tm_hour);
printf("分:%d\n", t.tm_min);
printf("秒:%d\n", t.tm_sec);
printf("夏令时:%d\n", t.tm_isdst);
//拼接成字符串
char msg[50];
sprintf_s(msg, "%d-%d-%d %d:%d:%d", t.tm_year + 1900, t.tm_mon + 1, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec);
printf(msg);
Lua中的代码
local now = os.date("%Y-%m-%d %H:%M:%S",os.time())