title: How do I get current date/time on the Windows command line in a suitable format for usage in a file/folder name?
body: <p>Update: Now that it's 2016 I'd use PowerShell for this unless there's a really compelling backwards-compatible reason for it, particularly because of the regional settings issue with using <code>date</code>. See @npocmaka's <a href="https://stackoverflow.com/a/19799236/8479">https://stackoverflow.com/a/19799236/8479</a></p>
  
  <hr>
  
  <p>What's a Windows command line statement(s) I can use to get the current datetime in a format that I can put into a filename?</p>
  
  <p>I want to have a .bat file that zips up a directory into an archive with the current date and time as part of the name, for example, <code>Code_2008-10-14_2257.zip</code>. Is there any easy way I can do this, independent of the regional settings of the machine?</p>
  
  <p>I don't really mind about the date format, ideally it'd be yyyy-mm-dd, but anything simple is fine.</p>
  
  <p>So far I've got this, which on my machine gives me <code>Tue_10_14_2008_230050_91</code>:</p>
  
  <pre><code>rem Get the datetime in a format that can go in a filename.
  set _my_datetime=%date%_%time%
  set _my_datetime=%_my_datetime: =_%
  set _my_datetime=%_my_datetime::=%
  set _my_datetime=%_my_datetime:/=_%
  set _my_datetime=%_my_datetime:.=_%
  
  rem Now use the timestamp by in a new ZIP file name.
  "d:\Program Files\7-Zip\7z.exe" a -r Code_%_my_datetime%.zip Code
  </code></pre>
  
  <p>I can live with this, but it seems a bit clunky. Ideally it'd be briefer and have the format mentioned earlier.</p>
  
  <p>I'm using Windows Server 2003 and Windows&nbsp;XP Professional. I don't want to install additional utilities to achieve this (although I realise there are some that will do nice date formatting).</p>