To do this in strict Batch, you have to use tokenization or substring extraction on %date%
. However, it's problematic because of changes due to locale settings and such. It's even more problematic when you want to get a relative date based on another date, because, as mentioned, there is no understanding in Batch of timespan calculation, considering months, years, leap years, daylight savings, etc.
So the best answer is usually to rely on an external language or library, like JScript in another answer, or my favorite: Powershell.
for /f %%a in ('"powershell [DateTime]::Now.AddDays(-4).ToString('dd-MMM-yy')"') do echo %%a
This is a one-liner that gets the date from Powershell, subtracts 4 days, and puts it in the dd-MMM-yy format as %%a
. You can do whatever you want with %%a
, and change the format as you like.