c++ 打印当前时间


#include 
#include 

using namespace std;


int main()
{
	// 基于当前系统的当前日期/时间
	time_t now = time(0);

	cout << "1970 到目前经过秒数:" << now << endl;

	tm *ltm = localtime(&now);

	char loc_date[20];

	sprintf(loc_date, "%d%02d%02d", 1900 + ltm->tm_year, 1 + ltm->tm_mon, ltm->tm_mday);
	delete ltm;
	std::cout << "location time: " << loc_date << std::endl;
	

	//// 输出 tm 结构的各个组成部分
	//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;
	
}

运行输出:
时间输出

你可能感兴趣的:(c与c++)