Format Windows command line date

The need for me to format date in the Windows command line environment came up when I needed to write a batch file to automatically copy the latest copies of certain files in a large folder. The files are named in the format of ???YYYYMMDD.txt, which presented itself as one easy way for me to query for latest files.

Built in to Windows command line is the %date% variable, which displays the system date as follows based on my regional setting.

Please note that your regional settings might be different. For example, for those in Australia, the date might be presented in a DD/MM/YYYY format rather than the typical US MM/DD/YYYY format. If this is the case, you may wish to adjust the code below accordingly.

To suit my needs, I need to format this date to YYYYMMDD. Knowing that %date%’s format is consistent in format, we can just parse it as a string.

This is nice and common knowledge.
However, what if you do logging on a device per device base and then send this logging to one fileshare.
The formatting is just a mess because different user have different regional settings in a large enterprise

Thanks C. Peter Chen. I needed to write a cmd for a Windows server that would archive log files with date-time stamps whenever my application is stop-started. Your example above got me started on the right foot. Below is the date-time string I’m using, thanks to you: