Yes, there are complicated pure Batch file methods that convert a date to Julian Day Number, subtract the number of days and convert the result back to date. There are also other methods that use the date functions of other programming languages, like PowerShell or VBScript/JScript as shown in the other answers.
I hope that the six-line pure Batch method below don't look so complicated.
@echo off
setlocal EnableDelayedExpansion
set N=4
set i=100
for %%a in (31 28 31 30 31 30 31 31 30 31 30 31) do set /A "i+=1" & set "dpm[!i!]=%%a"
for /F "tokens=1-3 delims=/" %%a in ("%date%") do (
set /A "DD=1%%b-N, I=^!(((DD-101)>>31)+1), MM=1%%a-I, J=^!(MM-100), MM+=J*12"
set /A "YYYY=%%c-J, dpm[102]+=^!(YYYY%%4), DD+=I*dpm[!MM!]"
)
set "newDate=%MM:~1%/%DD:~1%/%YYYY%
echo %newDate%
This method works with a %DATE%
format of MM/DD/YYYY; if your date format is different, just change the position of %%a
and %%b
parameters in the expression and the order of MM
and DD
in the result.
This method can subtract a maximum of one month from the current date.