How to get mm-dd-yyyy in DOS batch file?

28,314

Solution 1

This works independent of regional date/time format:

for /f %%I in ('wmic os get localdatetime ^|find "20"') do set dt=%%I
REM dt format is now YYYYMMDDhhmmss...
set dt=%dt:~4,2%-%dt:~2,2%-%dt:~0,4%
echo %dt%

Solution 2

set "$date=%date:~4%"
set "$date=%$date:/=-%"
echo %$date%
Share:
28,314
user167908
Author by

user167908

Updated on July 09, 2022

Comments

  • user167908
    user167908 almost 2 years

    In a batch file I need to get a file from a folder that has today's date.

    The problem is I can't get the batch date command to return the proper format.

    I have this: echo %date:/=-%

    But that always returns for example: Fri 06-20-2014

    What's the proper call for just returning: 06-20-2014

    Looked all over can't find.

    Thanks!