欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

C++获取当前时间字符串

程序员文章站 2022-07-14 19:46:18
...
#include <iostream>

std::string GetNowTime() {
	time_t setTime;
	time(&setTime);
	tm* ptm = localtime(&setTime);
	std::string time = std::to_string(ptm->tm_year + 1900)
	                   + "/"
	                   + std::to_string(ptm->tm_mon + 1)
	                   + "/"
	                   + std::to_string(ptm->tm_mday)
	                   + " "
	                   + std::to_string(ptm->tm_hour) + ":"
	                   + std::to_string(ptm->tm_min) + ":"
	                   + std::to_string(ptm->tm_sec);		
	return time;	
}

//test
int main(int argc, char *argv[]) {
	std::string strTime = GetNowTime();
	std::cout << "now time: " << strTime << std::endl;
	return 0;
}