- //由于使用了windows.h,所以这种方法只能在windows系统下使用,不能在Mac或linux下使用
#include "iostream"
#include "windows.h"
#include "string"
using namespace std;
int main(){
SYSTEMTIME ct;
GetLocalTime(&ct);;//如果用GetSystemTime(&ct),则获取的是格林尼治时间
int year = ct.wYear;
int month = ct.wMonth;
int day = ct.wDay;
int hour = ct.wHour;
int minute = ct.wMinute;
int second = ct.wSecond;
cout << year << "/" << month << "/" << day << " "
<< hour << ":" << minute << ":" << second << endl;
//把时间转换成字符串
char tmp[100];
sprintf_s(tmp, "%d/%d/%d %d:%d:%d", year, month, day, hour, minute, second);
string s = tmp;
cout << s << endl;
cout << tmp << endl;
return 0;
}