C++中并没有针对时间特意提供特定的时间类型,而是直接继承了C语言的结构以及函数,因此在C++中使用时间函数需要引用<ctime>头文件。

//时间的结构体
struct tm {
	int tm_sec;   // 秒,正常范围从 0 到 59,但允许至 61
	int tm_min;   // 分,范围从 0 到 59
	int tm_hour;  // 小时,范围从 0 到 23
	int tm_mday;  // 一月中的第几天,范围从 1 到 31
	int tm_mon;   // 月,范围从 0 到 11
	int tm_year;  // 自 1900 年起的年数
	int tm_wday;  // 一周中的第几天,范围从 0 到 6,从星期日算起
	int tm_yday;  // 一年中的第几天,范围从 0 到 365,从 1 月 1 日算起
	int tm_isdst; // 夏令时
};

 编译环境VS2019 

#include <iostream>
#include <ctime>
using namespace 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(&ltm , &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 << ":"<< ltm.tm_sec << endl;
	system("pause");
	return 0;
}

效果