Skip to main content

How to Subtract Days from a Date in Batch Script

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.

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%
pause

Subtracting 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%
pause
Why PowerShell Is Recommended

PowerShell's [datetime] type natively handles leap years, month boundaries, and year rollovers. Passing a negative value to AddDays() is all that distinguishes subtraction from addition, no formula changes or special cases required.

Method 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!
pause
Preserving Input Variables

The JDN conversion requires adjusting the year and month for January and February (subtracting 1 from the year and adding 12 to the month). The original variables y and m must not be modified during this step because they are used in the JDN formula alongside the adjusted values. This script uses separate adjY and adjM variables for the adjustment to keep the original y, m, and d intact for the calculation.

Why Subtract Days from a Date?

  1. Retention Policies: Finding all log files created before "Today minus 90 days" for automated deletion.
  2. Billing Cycles: Calculating the start date of a 30-day billing period that ends today.
  3. 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"
When to Use forfiles vs. Date Math

forfiles is purpose-built for file age comparisons and requires no date calculation at all. Use it when you only need to act on files by age. Use the date math approach when you need the actual computed date as a string. For example, to name an archive folder, write to a log, or pass to another command.

Important Considerations

Year and Month Boundaries

Subtracting 5 days from January 3 correctly produces December 29 of the previous year. Both the Julian Day formula and PowerShell handle year rollovers and month boundary crossings automatically.

Leap Years

February 29 is handled correctly by both the Julian Day formula and PowerShell. Subtracting 1 day from 2024-03-01 produces 2024-02-29 (leap year), while the same operation on 2023-03-01 produces 2023-02-28.

Date Range Limits

The Julian Day formula works for dates in the common era. Ensure your subtraction does not produce dates before the calendar's valid range. In practice, this is only a concern for extreme offsets subtracted from dates near year 1.

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.