title: Formatting dates with stringstream
body: <p>I want to avoid the usage of sprintf in my C++ code so I can use only std strings from C++, but I haven't found the way to replace it yet. I currently use sprintf to format dates and times like this:</p>
  
  <pre><code>char myDate[DATE_LENGTH]{};
  sprintf(myDate, "%4d-%02d-%02d %02d:%02d:%02d", year, month, day,
          hour, minute, second);
  </code></pre>
  
  <p>In this way, I will get a fixed length for each integer, with leading zeros if needed. </p>
  
  <p>I have searched how to replace this with stringstream, and found this:</p>
  
  <pre><code>std::ostringstream ss;
  ss &lt;&lt; std::setw(5) &lt;&lt; std::setfill('0') &lt;&lt; 12 &lt;&lt; "\n";
  </code></pre>
  
  <p>But this only would format one of the integers. I would need to do this for each date and tiem component and then append all of them together. So to replace one line of C-style code, I would need much more new code.</p>
  
  <p>Is not there any way to do better than this?</p>