Current date and time as string

匿名 (未验证) 提交于 2019-12-03 02:04:01

问题:

I wrote a function to get a current date and time in format: DD-MM-YYYY HH:MM:SS. It works but let's say, its pretty ugly. How can I do exactly the same thing but simpler?

string currentDateToString() {     time_t now = time(0);     tm *ltm = localtime(&now);      string dateString = "", tmp = "";     tmp = numToString(ltm->tm_mday);     if (tmp.length() == 1)         tmp.insert(0, "0");     dateString += tmp;     dateString += "-";     tmp = numToString(1 + ltm->tm_mon);     if (tmp.length() == 1)         tmp.insert(0, "0");     dateString += tmp;     dateString += "-";     tmp = numToString(1900 + ltm->tm_year);     dateString += tmp;     dateString += " ";     tmp = numToString(ltm->tm_hour);     if (tmp.length() == 1)         tmp.insert(0, "0");     dateString += tmp;     dateString += ":";     tmp = numToString(1 + ltm->tm_min);     if (tmp.length() == 1)         tmp.insert(0, "0");     dateString += tmp;     dateString += ":";     tmp = numToString(1 + ltm->tm_sec);     if (tmp.length() == 1)         tmp.insert(0, "0");     dateString += tmp;      return dateString; } 

回答1:

Non C++11 solution: With the header, you could use strftime. Make sure your buffer is large enough, you wouldn't want to overrun it and wreak havoc later.

#include  #include   int main () {   time_t rawtime;   struct tm * timeinfo;   char buffer[80];    time (&rawtime);   timeinfo = localtime(&rawtime);    strftime(buffer,sizeof(buffer),"%d-%m-%Y %I:%M:%S",timeinfo);   std::string str(buffer);    std::cout 


回答2:

Since C++11 you could use std::put_time from iomanip header:

#include  #include  #include   int main() {     auto t = std::time(nullptr);     auto tm = *std::localtime(&t);     std::cout 

std::put_time is a stream manipulator, therefore it could be used together with std::ostringstream in order to convert the date to a string:

#include  #include  #include  #include   int main() {     auto t = std::time(nullptr);     auto tm = *std::localtime(&t);      std::ostringstream oss;     oss 


回答3:

you can use asctime() function of time.h to get a string simply .

time_t _tm =time(NULL );  struct tm * curtime = localtime ( &_tm ); cout

Sample output:

The current date/time is:Fri Oct 16 13:37:30 2015 


回答4:

I wanted to use the C++11 answer, but I could not because GCC 4.9 does not support std::put_time.

std::put_time implementation status in GCC?

I ended up using some C++11 to slightly improve the non-C++11 answer. For those that can't use GCC 5, but would still like some C++11 in their date/time format:

 std::array buffer;  buffer.fill(0);  time_t rawtime;  time(&rawtime);  const auto timeinfo = localtime(&rawtime);  strftime(buffer.data(), sizeof(buffer), "%d-%m-%Y %H-%M-%S", timeinfo);  std::string timeStr(buffer.data()); 


回答5:

Using C++ in MS Visual Studio 2015 (14), I use:

#include   string NowToString() {   chrono::system_clock::time_point p = chrono::system_clock::now();   time_t t = chrono::system_clock::to_time_t(p);   char str[26];   ctime_s(str, sizeof str, &t);   return str; } 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!