Cpp獲取當前時間字符串

#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;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章