While adding days to a date projects into the future, subtracting days looks into the past. This is essential for building cleanup scripts that need to find "Files older than 30 days," calculating billing periods, or determining the start date of a time window based on today's date. In Batch, date subtraction follows the same logic as addition, but with a negative offset.
In this guide, we will demonstrate how to subtract days from a date using the recommended PowerShell bridge and a pure Batch alternative.
Method 1: The PowerShell Bridge (Recommended)
PowerShell's AddDays() method accepts negative numbers, making subtraction trivial.
Implementation Script
@echo off
setlocal
:: What date was 30 days ago?
for /f "usebackq delims=" %%A in (`
powershell -NoProfile -Command "(Get-Date).AddDays(-30).ToString('yyyy-MM-dd')"
`) do set "pastDate=%%A"
echo 30 days ago was: %pastDate%
pauseSubtracting from a Specific Date
@echo off
setlocal
set "startDate=2024-03-01"
set "daysBack=60"
for /f "usebackq delims=" %%A in (`
powershell -NoProfile -Command "([datetime]'%startDate%').AddDays(-%daysBack%).ToString('yyyy-MM-dd')"
`) do set "result=%%A"
echo %startDate% minus %daysBack% days = %result%
pauseMethod 2: Pure Batch (Julian Day Number)
Using the same Julian Day approach from the "Add Days" article, simply subtract the offset instead of adding it.
Implementation Script
@echo off
setlocal enabledelayedexpansion
:: Input
set "y=2024" & set "m=3" & set "d=1"
set "subDays=60"
echo Input: !y!-!m!-!d! - !subDays! days
:: 1. Convert to Julian Day Number
:: Adjust year and month for January/February
set "adjY=!y!"
set "adjM=!m!"
if !adjM! LEQ 2 (
set /a "adjY=y - 1"
set /a "adjM=m + 12"
)
set /a "jdn=(1461 * (adjY + 4800)) / 4 + (367 * (adjM - 2)) / 12 - (3 * ((adjY + 4900) / 100)) / 4 + d - 32075"
:: 2. SUBTRACT the offset
set /a "jdn-=subDays"
:: 3. Convert JDN back to Calendar Date
set /a "l=jdn + 68569"
set /a "n=4 * l / 146097"
set /a "l=l - (146097 * n + 3) / 4"
set /a "i=4000 * (l + 1) / 1461001"
set /a "l=l - 1461 * i / 4 + 31"
set /a "j=80 * l / 2447"
set /a "d=l - 2447 * j / 80"
set /a "l=j / 11"
set /a "m=j + 2 - 12 * l"
set /a "y=100 * (n - 49) + i + l"
:: 4. Zero-pad month and day
if !m! LSS 10 set "m=0!m!"
if !d! LSS 10 set "d=0!d!"
echo Result: !y!-!m!-!d!
pauseWhy Subtract Days from a Date?
- Retention Policies: Finding all log files created before "Today minus 90 days" for automated deletion.
- Billing Cycles: Calculating the start date of a 30-day billing period that ends today.
- Audit Windows: Generating a report of all system changes that occurred "Within the last 7 days."
Pro Tip: Using forfiles for File-Based Lookbacks
If your goal is specifically to find files older than X days, Windows has a built-in command:
:: Delete .log files older than 30 days
forfiles /P "C:\Logs" /S /M *.log /D -30 /C "cmd /c del @file"Important Considerations
Conclusion
Subtracting days from a date is the complementary skill to date addition, completing your toolkit for full calendar arithmetic. By mastering both directions, you can calculate any date window, past or future, with confidence. This capability is essential for building professional retention policies, audit scripts, and compliance tools that operate on strict time-based rules.