方法一 、调用time.h
#include <time.h>
time_t t_now;
struct tm pt;
time(&t_now);
//tm *pt = localtime(&t);
localtime_s(&pt,&t_now);
int year = pt.tm_year + 1900;
int month = pt.tm_mon + 1;
int day = pt.tm_mday;
int hour = pt.tm_hour;
int minute = pt.tm_min;
int second = pt.tm_sec;
std::cout << year << "-" << month << "-" << day << " ; " << hour << " ; " << minute << " ; " << second << std::endl;
char tick[_MAX_PATH];
strftime(tick, _countof(tick), "%Y%m%d_%H%M%S", &pt);
std::cout << tick << std::endl;
二、调用window.h
#include <windows.h>
#include <stdio.h>
SYSTEMTIME sys;
GetLocalTime(&sys);
printf("%4d-%02d-%02d %02d:%02d:%02d.%03d 星期%1d\n", sys.wYear, sys.wMonth, sys.wDay, sys.wHour, sys.wMinute, sys.wSecond, sys.wMilliseconds, sys.wDayOfWeek);