Windows Batch File Current Date Formatting

Often you will create a windows batch file that runs on a periodic basis, and ideally you’d like it to reflect the current date when it does things like create files and folders.

I’ve seen some fairly arcane methods of retrieving and storing this information in variables through the years, but I think I’ve just found the most elegant solution.

To store today’s date in a variable with format yyyymmdd:
SET VAR=%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%

That’s it! This is pretty hard to find online. It seems to be usually part of the more complete expositions on variable expansion, and is described as String Substitutions or Substrings. Obviously you can change the offsets above to change your format, and also note the character position pointer is zero-based.

Leave a Reply

Your email address will not be published. Required fields are marked *