Skip to main content

How to Calculate Age from a Birthdate in Batch Script

Calculating a person's age from their birthdate requires comparing the birth year, month, and day against the current date. The tricky part is accounting for whether the person has already had their birthday this year. If today's month/day is before the birth month/day, you must subtract one from the raw year difference.

In this guide, we will demonstrate how to calculate age accurately using date comparison logic.

The Strategy: Year Difference with Birthday Check

  1. Get today's date (Year, Month, Day).
  2. Get the birthdate (Year, Month, Day).
  3. Calculate Age = CurrentYear - BirthYear.
  4. If today's date is before the birthday this year, subtract 1.

Implementation Script

@echo off
setlocal enabledelayedexpansion

:: 1. Get today's date (via WMIC for reliability)
for /f "tokens=2 delims==" %%I in ('wmic os get localdatetime /value') do set "dt=%%I"
set "curY=!dt:~0,4!"
set /a "curM=1!dt:~4,2! - 100"
set /a "curD=1!dt:~6,2! - 100"

:: 2. Input the birthdate
set /p "birthY=Enter birth year (YYYY): "
set /p "birthM=Enter birth month (MM): "
set /p "birthD=Enter birth day (DD): "

:: Strip leading zeros for math
set /a "bY=birthY"
set /a "bM=1!birthM! - 100"
set /a "bD=1!birthD! - 100"

:: 3. Validate: Birthdate must not be in the future
set /a "curDate=curY * 10000 + curM * 100 + curD"
set /a "birthDate=bY * 10000 + bM * 100 + bD"
if !birthDate! GTR !curDate! (
echo [ERROR] Birthdate is in the future.
pause
exit /b
)

:: 4. Calculate Raw Age
set /a "age=curY - bY"

:: 5. Birthday Check: Has this year's birthday already passed?
if !curM! LSS !bM! (
set /a "age-=1"
) else if !curM! EQU !bM! (
if !curD! LSS !bD! (
set /a "age-=1"
)
)

:: 6. Format display values (zero-pad month and day)
set "dispBM=!birthM!"
set "dispBD=!birthD!"
if !bM! LSS 10 if "!birthM:~0,1!" NEQ "0" set "dispBM=0!bM!"
if !bD! LSS 10 if "!birthD:~0,1!" NEQ "0" set "dispBD=0!bD!"

echo.
echo ==========================================
echo BIRTHDATE: !birthY!-!dispBM!-!dispBD!
echo TODAY: !dt:~0,4!-!dt:~4,2!-!dt:~6,2!
echo AGE: !age! years
echo ==========================================
pause
WMIC Deprecation

wmic is deprecated in newer versions of Windows. As a future-proof alternative, you can retrieve the date string using PowerShell:

for /f "usebackq delims=" %%I in (`powershell -NoProfile -Command "Get-Date -Format 'yyyyMMddHHmmss'"`) do set "dt=%%I"

Why Calculate Age in Batch?

  1. Account Management: Determining if a user account or system certificate is "older than 365 days" for mandatory renewal.
  2. Hardware Auditing: Calculating how old a server or workstation is based on its installation date to determine if it needs to be retired.
  3. Compliance: Verifying that a user meets a minimum age requirement based on their profile data.

Important Considerations

Leap Year Birthdays

If someone was born on February 29 and the current year is not a leap year, the script handles this gracefully because it compares month and day separately. On March 1 of a non-leap year, the month comparison (3 > 2) determines the birthday has passed, producing the correct age without needing to validate whether February 29 exists in the current year.

Leading Zero Octal Trap

Batch treats numbers with leading zeros as octal. The month value 08 and 09 from date strings are invalid octal numbers and cause errors with set /a. The trick set /a "curM=1%mm% - 100" prepends a 1 to create a valid decimal number (e.g., 108), then subtracts 100 to get the correct value (8).

Extending to Months and Days

You can extend this script to also show "X months and Y days" until the next birthday for a more detailed output. Calculate the month difference (birthM - curM) and day difference (birthD - curD), adjusting for negative values by borrowing from the next higher unit.

Conclusion

Calculating age from a birthdate is a practical exercise in multi-field date comparison. By separating the logic into "Year Difference" and "Birthday Adjustment," you build a robust calculator that handles all edge cases. This capability is essential for building account auditing tools, hardware lifecycle managers, and compliance scripts that need to make time-aware decisions.