Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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!

like image 729
user167908 Avatar asked Dec 01 '22 16:12

user167908


2 Answers

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:~6,2%-%dt:~0,4%
echo %dt%
like image 98
Stephan Avatar answered Dec 15 '22 14:12

Stephan


set "$date=%date:~4%"
set "$date=%$date:/=-%"
echo %$date%
like image 40
SachaDee Avatar answered Dec 15 '22 14:12

SachaDee