maphew

Batch snippets

December 2, 2020 

DOS, CMD, Command line 

You can get the current date in a locale-agnostic way using

for /f "skip=1" %%x in ('wmic os get localdatetime') do if not defined MyDate set MyDate=%%x

Then you can extract the individual parts using substrings:

set today=%MyDate:~0,4%-%MyDate:~4,2%-%MyDate:~6,2%

Another way, where you get variables that contain the individual parts, would be:

for /f %%x in ('wmic path win32_localtime get /format:list ^| findstr "="') do set %%x 
set today=%Year%-%Month%-%Day%

From < https://stackoverflow.com/questions/10945572/windows-batch-formatted-date-into-variable