title: Current date and time as string
body: <p>I wrote a function to get a current date and time in format: <code>DD-MM-YYYY HH:MM:SS</code>. It works but let's say, its pretty ugly. How can I do <strong>exactly the same thing</strong> but simpler?</p>
  
  <pre><code>string currentDateToString()
  {
      time_t now = time(0);
      tm *ltm = localtime(&amp;now);
  
      string dateString = "", tmp = "";
      tmp = numToString(ltm-&gt;tm_mday);
      if (tmp.length() == 1)
          tmp.insert(0, "0");
      dateString += tmp;
      dateString += "-";
      tmp = numToString(1 + ltm-&gt;tm_mon);
      if (tmp.length() == 1)
          tmp.insert(0, "0");
      dateString += tmp;
      dateString += "-";
      tmp = numToString(1900 + ltm-&gt;tm_year);
      dateString += tmp;
      dateString += " ";
      tmp = numToString(ltm-&gt;tm_hour);
      if (tmp.length() == 1)
          tmp.insert(0, "0");
      dateString += tmp;
      dateString += ":";
      tmp = numToString(1 + ltm-&gt;tm_min);
      if (tmp.length() == 1)
          tmp.insert(0, "0");
      dateString += tmp;
      dateString += ":";
      tmp = numToString(1 + ltm-&gt;tm_sec);
      if (tmp.length() == 1)
          tmp.insert(0, "0");
      dateString += tmp;
  
      return dateString;
  }
  </code></pre>