vs2017 上使用安全方法:
#include
#include
usingnamespace std;
#define TIMESIZE 26
int main(){
// 时间
time_t now = time(0);
char dt[TIMESIZE];
errno_t err;
err = ctime_s(dt, TIMESIZE,&now);
cout <<"local time: "<< dt << endl;
cout <<"timestamp: "<< now << endl;
struct tm ltm;
localtime_s(<m,&now);
cout <<"年: "<<1900+ ltm.tm_year << endl;
cout <<"月: "<<1+ ltm.tm_mon << endl;
cout <<"日: "<< ltm.tm_mday << endl;
cout <<"时间: "<< ltm.tm_hour <<":";
cout << ltm.tm_min <<":";
cout << ltm.tm_sec << endl;
return0;
}