Skip to content

How to Add Days to a Date in Batch Script

Learn reliable methods to add days to a date in Windows Batch scripts, including PowerShell's AddDays() and the Julian Day Number formula for handling leap years, month boundaries, and edge cases.

Date arithmetic is one of the most challenging tasks in Batch because the Command Prompt has no built-in date math engine. You must account for varying month lengths, leap years, and year boundaries. The most reliable approach is to convert a date to a serial "Day Number" (like the Julian Day Number), add your desired offset, and convert it back.

In this guide, we will demonstrate how to add days to a date using the Julian Day Number formula via PowerShell for reliability.

PowerShell has a native AddDays() method that handles all edge cases perfectly.

Implementation Script

@echo off
setlocal

:: Add 30 days to today's date
for /f "usebackq delims=" %%A in (`
    powershell -NoProfile -Command "(Get-Date).AddDays(30).ToString('yyyy-MM-dd')"
`) do set "futureDate=%%A"

echo Today + 30 days = %futureDate%
pause

Adding to a Specific Date

@echo off
setlocal

set "startDate=2024-02-15"
set "daysToAdd=45"

for /f "usebackq delims=" %%A in (`
    powershell -NoProfile -Command "([datetime]'%startDate%').AddDays(%daysToAdd%).ToString('yyyy-MM-dd')"
`) do set "result=%%A"

echo %startDate% + %daysToAdd% days = %result%
pause

Method 2: The Native Batch Approach (Julian Day)

For environments where PowerShell is unavailable, you can use the Julian Day Number formula. This is significantly more complex but works in pure Batch.

Implementation Script

@echo off
setlocal enabledelayedexpansion

:: Input Date and Offset
set "y=2024" & set "m=2" & set "d=15"
set "addDays=45"

echo Input: !y!-!m!-!d! + !addDays! days

:: 1. Convert date to Julian Day Number (JDN)
:: 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. Add the offset
set /a "jdn+=addDays"

:: 3. Convert JDN back to a 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

Why Add Days to a Date?

  1. Expiration Dates: Calculating when a license, certificate, or trial period will expire based on the installation date.
  2. Retention Policies: Determining the cutoff date for deleting files older than "90 days."
  3. Scheduling: Computing the date for a future maintenance window or deadline.

Important Considerations

Conclusion

Adding days to a date is a critical capability for scheduling and retention scripts.

While the native Julian Day approach proves that it is possible in pure Batch, the PowerShell bridge is the recommended standard for professional use due to its accuracy and simplicity.

By mastering date arithmetic, you ensure your automation can handle time-based logic with the precision needed for compliance, licensing, and maintenance planning.

Navigation