title: Format date and time in a Windows batch script
body: <p>In a Windows (Windows&nbsp;XP) batch script I need to format the current date and time for later use in files names, etc.</p>
  
  <p>It is similar to Stack Overflow question <em><a href="https://stackoverflow.com/questions/864718/how-to-append-a-date-in-batch-files">How to append a date in batch files</a></em>, but with time in as well.</p>
  
  <p>I have this so far:</p>
  
  <pre><code>echo %DATE%
  echo %TIME%
  set datetimef=%date:~-4%_%date:~3,2%_%date:~0,2%__%time:~0,2%_%time:~3,2%_%time:~6,2%
  echo %datetimef%
  </code></pre>
  
  <p>which gives:</p>
  
  <pre><code>28/07/2009
   8:35:31.01
  2009_07_28__ 8_36_01
  </code></pre>
  
  <p>Is there a way I can allow for a single digit hour in %TIME%, so I can get the following?</p>
  
  <pre><code>2009_07_28__08_36_01
  </code></pre>