<legend id='eg0T5'><style id='eg0T5'><dir id='eg0T5'><q id='eg0T5'></q></dir></style></legend>
          <bdo id='eg0T5'></bdo><ul id='eg0T5'></ul>

        <tfoot id='eg0T5'></tfoot>
      1. <i id='eg0T5'><tr id='eg0T5'><dt id='eg0T5'><q id='eg0T5'><span id='eg0T5'><b id='eg0T5'><form id='eg0T5'><ins id='eg0T5'></ins><ul id='eg0T5'></ul><sub id='eg0T5'></sub></form><legend id='eg0T5'></legend><bdo id='eg0T5'><pre id='eg0T5'><center id='eg0T5'></center></pre></bdo></b><th id='eg0T5'></th></span></q></dt></tr></i><div id='eg0T5'><tfoot id='eg0T5'></tfoot><dl id='eg0T5'><fieldset id='eg0T5'></fieldset></dl></div>
      2. <small id='eg0T5'></small><noframes id='eg0T5'>

        当前日期和时间作为字符串

        时间:2023-06-30

          <legend id='WTuiJ'><style id='WTuiJ'><dir id='WTuiJ'><q id='WTuiJ'></q></dir></style></legend><tfoot id='WTuiJ'></tfoot>
              <tbody id='WTuiJ'></tbody>
            <i id='WTuiJ'><tr id='WTuiJ'><dt id='WTuiJ'><q id='WTuiJ'><span id='WTuiJ'><b id='WTuiJ'><form id='WTuiJ'><ins id='WTuiJ'></ins><ul id='WTuiJ'></ul><sub id='WTuiJ'></sub></form><legend id='WTuiJ'></legend><bdo id='WTuiJ'><pre id='WTuiJ'><center id='WTuiJ'></center></pre></bdo></b><th id='WTuiJ'></th></span></q></dt></tr></i><div id='WTuiJ'><tfoot id='WTuiJ'></tfoot><dl id='WTuiJ'><fieldset id='WTuiJ'></fieldset></dl></div>

              <small id='WTuiJ'></small><noframes id='WTuiJ'>

                • <bdo id='WTuiJ'></bdo><ul id='WTuiJ'></ul>

                  本文介绍了当前日期和时间作为字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我编写了一个函数来获取当前日期和时间的格式:DD-MM-YYYY HH:MM:SS.它有效,但让我们说,它非常丑陋.我怎样才能做完全相同的事情但更简单?

                  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;
                  }
                  

                  推荐答案

                  Non C++11 solution: with the header, you can use strftime.确保您的缓冲区足够大,您不希望它溢出并在以后造成严重破坏.

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

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

                  这篇关于当前日期和时间作为字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:如何以“月份日期 hh:mm:ss"格式比较两个时间戳检查 +ve 或 -ve 值 下一篇:如何在C++中实现函数的超时

                  相关文章

                  1. <i id='yvw7G'><tr id='yvw7G'><dt id='yvw7G'><q id='yvw7G'><span id='yvw7G'><b id='yvw7G'><form id='yvw7G'><ins id='yvw7G'></ins><ul id='yvw7G'></ul><sub id='yvw7G'></sub></form><legend id='yvw7G'></legend><bdo id='yvw7G'><pre id='yvw7G'><center id='yvw7G'></center></pre></bdo></b><th id='yvw7G'></th></span></q></dt></tr></i><div id='yvw7G'><tfoot id='yvw7G'></tfoot><dl id='yvw7G'><fieldset id='yvw7G'></fieldset></dl></div>
                  2. <small id='yvw7G'></small><noframes id='yvw7G'>

                    • <bdo id='yvw7G'></bdo><ul id='yvw7G'></ul>

                      <tfoot id='yvw7G'></tfoot>
                      <legend id='yvw7G'><style id='yvw7G'><dir id='yvw7G'><q id='yvw7G'></q></dir></style></legend>