Compare 2 dates in a Windows batch file

2019-01-12 05:12发布

问题:

How is it possible to compare 2 dates in batch files? This:

if %%newdate geq %olddate%  _do smth_

does not work.

In this case I have that
27.05.2013 is greater than 15.07.2013 and
14.07.2013 is less than 15.07.2013

Zhenya

回答1:

try this:

set "sdate1=%olddate:~-4%%olddate:~3,2%%olddate:~0,2%"
set "sdate2=%newdate:~-4%%newdate:~3,2%%newdate:~0,2%"
if %sdate1% GTR %sdate2% (goto there)  else echo here


回答2:

call :date_to_number %date1% date1
call :date_to_number %date2% date2

if %date1% GEQ %date2% echo date1 is bigger

goto :eof
:date_to_number
setlocal
if "%~1" EQU "" goto :eof
for /f "tokens=1,2,3 delims=." %%D in ("%~1") do (
  set "the_date=%%F%%E%%D"   
)
endlocal & if "%~2" neq "" (set %~2=%the_date%) else echo %the_date%
goto :eof

But this will work only with dates in format DD.MM.YYYY